Static overloaded join(…) methods were added in JDK 1.8 and have below declarations::
1. public static String join(CharSequence delimiter, CharSequence… elements) {…}: It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
For example,
String.join(".", "A", "B", "C"); //returns "A.B.C"
String.join("+", new String[]{"1", "2", "3"}); //returns "1+2+3"
String.join("-", "HELLO"); //returns "HELLO"
If delimiter is null or elements refer to null, then NullPointerException is thrown. e.g.,
String.join(null, "A", "B"); //throws NullPointerException
String [] arr = null;
String.join("-", arr); //throws NullPointerException
But if single element is null, then “null” is considered. e.g.,
String str = null;
String.join("-", str); //returns "null"
String.join("::", new String[] {"James", null, "Gosling"}); //returns "James::null::Gosling"
2. public static String join(CharSequence delimiter, Iterable elements) {…}: It returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
For example,
String.join(".", List.of("A", "B", "C")); //returns "A.B.C"
String.join(".", List.of("HELLO")); //returns "HELLO"
If delimiter is null or elements refer to null, then NullPointerException is thrown. e.g.,
String.join(null, List.of("HELLO")); //throws NullPointerException
List<String> list = null;
String.join("-", list); //throws NullPointerException
But if single element is null, then “null” is considered. e.g.,
List<String> list = new ArrayList<>();
list.add("A");
list.add(null);
String.join("::", list); //returns "A::null"
Please note: String.join("-", null);
causes compilation error as compiler is unable to tag this call to specific join(…) method. It is an ambiguous call.