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);
Console output

Java, Spring, SQL

  1. The first argument, ", " is the delimiter.
  2. The values to join follow the delimiter.
  3. Java inserts the delimiter only between values, never at the start or end.
  4. join() returns a new String, 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

String.join() variants
SyntaxPurpose
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);
Console output

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);
Console output

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 + "]");
Console output

[]
[Java]

  1. With no elements to join, the result is an empty string.
  2. With one element, that element is returned without a delimiter.
  3. A delimiter appears only between adjacent elements.

What happens with null?

String tekstas = String.join(", ", "Java", null, "SQL");

System.out.println(tekstas);
Console output

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);
Result

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);
Console output

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());
Console output

[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 a NullPointerException.
  • Expecting null elements will be skipped even though they are written as text, null.
  • Trying to pass a collection whose elements are not CharSequence values.
  • Using join()when more complex formatting is needed; a better fit may be String.format(), StringJoiner or 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.