VOOZH about

URL: https://www.geeksforgeeks.org/java/stringjoiner-merge-method-in-java/

⇱ StringJoiner merge() method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

StringJoiner merge() method in Java

Last Updated : 11 Jul, 2025

The merge(StringJoiner other) of StringJoiner adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty. If the given StringJoiner is empty, the call has no effect.
Syntax: 
 

public StringJoiner merge(StringJoiner other)


Parameters: This method accepts a mandatory parameter other which is the StringJoiner whose contents should be merged into this one
Returns: This method returns this StringJoiner
Exception: This method throws NullPointerException if the other StringJoiner is null
Below programs illustrate the merge() method:
Example 1: To demonstrate merge() with delimiter " "
 


Output: 
StringJoiner 1: Geeks for Geeks
StringJoiner 2: A Computer Portal
Merged StringJoiner : Geeks for Geeks A Computer Portal

 

Example 2: To demonstrate merge() with delimiter ", "
 


Output: 
StringJoiner 1: Geeks, for, Geeks
StringJoiner 2: A, Computer, Portal
Merged StringJoiner : Geeks, for, Geeks, A, Computer, Portal

 

Example 3: To demonstrate NullPointerException
 


Output: 
StringJoiner 1: Geeks, for, Geeks
StringJoiner 2: null
Exception during merge: java.lang.NullPointerException

 
Comment