Java String.join() MethodLast 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. SignatureBoth 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. Parametersdelimiter : char value to be added with each element elements : char value to be attached with delimiter Returnsjoined string with delimiter Exception ThrowsNullPointerException if element or delimiter is null. Since1.8 Internal ImplementationExample: Java String.join() MethodExampleCompile and RunOutput: welcome-to-TpointTech 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() MethodWe can use a delimiter to format the string as we did in the example below to show the date and time. ExampleCompile and RunOutput: 25/06/2018 12:10:10 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() MethodIn the case of using null as a delimiter, we get the NullPointerException. The following example depicts the same. ExampleCompile and RunOutput: 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. ExampleCompile and RunOutput: 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() MethodIf 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. ExampleCompile and RunOutput: 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(). Next TopicJava String lastIndexOf() |
We request you to subscribe our newsletter for upcoming updates.