STRING STATIC METHODS
Java String join() method
String.join() joins several text values into one String with a chosen delimiter between them. It is convenient for lists, export rows and clearly formatted text.
Your first join
String rezultatas = String.join(", ", "Java", "Spring", "SQL");
System.out.println(rezultatas);Java, Spring, SQL
- The first argument,
", "is the delimiter. - The values to join follow the delimiter.
- Java inserts the delimiter only between values, never at the start or end.
join()returns a newString, so the result can be saved in a variable.
Why is join() a static method?
join() is called through the class name, String, because it needs no existing string object. It receives the delimiter and values as arguments:
String tekstas = String.join(", ", "A", "B", "C");
On the left, String is the return type. String before the dot is the class that owns the static method.
Method syntax
| Syntax | Purpose |
|---|---|
String.join(CharSequence delimiter, CharSequence... elements) | Joins several individually supplied values. |
String.join(CharSequence delimiter, Iterable<? extends CharSequence> elements) | Joins values from a collection, such as List<String>. |
Both variants return a new String. CharSequence means the method accepts not only Stringbut also other text types, such as StringBuilder.
The delimiter controls the result
String suKableliu = String.join(", ", "obuoliai", "bananai", "kriaušės");
String suBruksniu = String.join(" - ", "pirmas", "antras", "trečias");
String beTarpu = String.join("", "J", "a", "v", "a");
System.out.println(suKableliu);
System.out.println(suBruksniu);
System.out.println(beTarpu);obuoliai, bananai, kriaušės
pirmas - antras - trečias
Java
", "creates a standard comma-separated list." - "suits text that needs clearer visual separation.- An empty delimiter,
""joins the strings with no space between them.
Joining a collection
import java.util.List;
List temos = List.of("kintamieji", "ciklai", "metodai");
String tekstas = String.join(" | ", temos);
System.out.println(tekstas);kintamieji | ciklai | metodai
When values are already in a collection, you need not retrieve each one manually. String.join() can traverse the Iterable values and combine them into one string in the same order.
An empty or single-element list
String tuscias = String.join(", ");
String vienas = String.join(", ", "Java");
System.out.println("[" + tuscias + "]");
System.out.println("[" + vienas + "]");[]
[Java]
- With no elements to join, the result is an empty string.
- With one element, that element is returned without a delimiter.
- A delimiter appears only between adjacent elements.
What happens with null?
String tekstas = String.join(", ", "Java", null, "SQL");
System.out.println(tekstas);Java, null, SQL
If an element being joined is null, Java writes the text nullinto the result. This can aid diagnostics, but user-facing text often needs such values replaced or removed beforehand.
The delimiter cannot be null
String tekstas = String.join(null, "Java", "SQL");
System.out.println(tekstas);NullPointerException
The first argument is the required delimiter. If it is null, the method has nothing to insert between values and throws a runtime NullPointerException.
join() instead of manual concatenation
String pirmas = "Java";
String antras = "Spring";
String trecias = "SQL";
String rankiniuBudu = pirmas + ", " + antras + ", " + trecias;
String suJoin = String.join(", ", pirmas, antras, trecias);
System.out.println(rankiniuBudu);
System.out.println(suJoin);Java, Spring, SQL
Java, Spring, SQL
When joining two or three clear values, the + operator is understandable. With more values or a collection, String.join() reduces repetition and avoids an extra trailing comma.
When you need a prefix and suffix
import java.util.StringJoiner;
StringJoiner joiner = new StringJoiner(", ", "[", "]");
joiner.add("Java");
joiner.add("Spring");
joiner.add("SQL");
System.out.println(joiner.toString());[Java, Spring, SQL]
String.join() inserts a delimiter between elements but has no separate prefix or suffix parameters. For those, use StringJoiner.
Common mistakes
- Forgetting that the first argument is the delimiter, not the first list item.
- Passing the delimiter as
null, producing aNullPointerException. - Expecting
nullelements will be skipped even though they are written as text, null. - Trying to pass a collection whose elements are not
CharSequencevalues. - Using
join()when more complex formatting is needed; a better fit may beString.format(),StringJoineror stream operations.
When should you use String.join()?
String.join() Choose it to combine several text values with the same delimiter. It suits menu items, tags, CSV-style rows, simple lists and turning collections into readable text.