String join()
Java String.join() Method
Last Updated : 12 Jan 2026
The String.join() method (introduced in JDK 1.8) is a static utility. It concatenates multiple strings with a specified delimiter and returns a single combined string. It is especially useful for building readable outputs or joining collections of strings.
Signature
Both versions yield a new string with the given delimiter separating each piece. It is important to remember that the delimiter appears at both the start and finish of the generated string, as well as in between every pair of adjacent components. Furthermore, a NullPointerException is thrown, per the Java documentation, in the event that either the element or the delimiter is null.
Parameters
delimiter : char value to be added with each element
elements : char value to be attached with delimiter
Returns
joined string with delimiter
Exception Throws
NullPointerException if element or delimiter is null.
Since
1.8
Internal Implementation
Example: Java String.join() Method
Output:
Explanation
The above program, concatenates the provided strings ("Welcome", "to", "TpointTech") with the designated delimiter "-" between them. As a result, "Welcome-to-TpointTech" would be the output string.
Example: Java String.join() Method
We can use a delimiter to format the string as we did in the example below to show the date and time.
Output:
Explanation
In the above program, two strings are formed using the String.join() function. First, the string "date" is created by concatenating the strings "25", "06", and "2018" with the delimiter "/". The result is "25/06/2018". System.out.print() is then used to print this string. Concatenating "12", "10", and "10" with ":" acting as the delimiter yields "12:10:10" when the second string, time, is used. System.out.println() is used to print this string, appending it to the preceding output without a pause.
Example: Java String.join() Method
In the case of using null as a delimiter, we get the NullPointerException. The following example depicts the same.
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.toString()" because "delimiter" is null at java.base/java.lang.String.join(String.java:3476) at Main.main(Main.java:7)
Explanation
When we execute the above program, a NullPointerException occurred. The error occurs as a result of the delimiter parameter being null, which is not permitted, being supplied to the String.join() method. Because the String.join() method needs a non-null value for the delimiter, it throws a NullPointerException when it comes across a null delimiter. A non-null delimiter, such as an empty string ("") or any other suitable character sequence, should be supplied as the first argument to the String.join() method in order to fix this problem.
However, if the elements that have to be attached to the delimiter are null, then we get the ambiguity. It is because there are two join() methods, and null is acceptable for both types of the String.join() method. Consider the following example.
Output:
Main.java:7: error: reference to join is ambiguous
str = String.join("India", null);
^
both method join(CharSequence,CharSequence...) in String and method join(CharSequence,Iterable<? extends CharSequence>) in String match
Main.java:7: warning: non-varargs call of varargs method with inexact argument type for last parameter;
str = String.join("India", null);
^
cast to CharSequence for a varargs call
cast to CharSequence[] for a non-varargs call and to suppress this warning
1 error
1 warning
Example: Java String.join() Method
If the elements that have to be attached with the delimiter have some strings, in which a few of them are null, then the null elements are treated as a normal string, and we do not get any exception or error. Let's understand it through an example.
Output:
null- wake up - eat - write content for Tpoint - eat - sleep
Explanation
In the above program, the String variable str to null. It concatenates multiple strings with"-" acting as the delimiter by using the String.join() method. The String.join() method handles one of the elements as a regular string even though it is null, preventing a NullPointerException. Concatenating all of the supplied strings-including the null element-separated by "-" yields the final string. Consequently, "null-wake up-eat-write content for Tpoint-eat-sleep" would be the program's output. This behaviour shows how null elements are treated as regular strings during concatenation by String.join().