Java String join() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, String.join(delimiter, elements) is a static method that returns a new String made of all the elements with the delimiter placed between them, never at the start or the end.

It accepts either individual values (varargs or an array) or any Iterable, such as a List. It is the opposite of split and replaces the classic loop that has to avoid a trailing comma.

join() is a static method of java.lang.String, added in Java 8. Call it on the class name, as in String.join(...). No import is needed. Requires Java 8 or newer.

Syntax

Call it on the class name with a delimiter followed by the elements.

java
String csv  = String.join(",", "a", "b", "c");   // "a,b,c"
String path = String.join("/", folders);          // any Iterable
CallWhat it does
String.join("-", "a", "b")"a-b".
String.join("", "x", "y")"xy".
String.join(", ", list)All list items, comma separated.

Elements can be any CharSequence: String, StringBuilder, and so on. To join numbers or other objects, convert them first, for example with a stream and Collectors.joining.

Library declarations
java
public static String join(CharSequence delimiter, CharSequence... elements);
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements);

Parameters

ParameterDescription
delimiterText placed between neighboring elements.
elementsThe values to join: varargs, an array, or an Iterable.

Return Value

Return ValueDescription
StringThe joined text; "" when there are no elements.

Java Examples

Example 1 — Join Values with Different Delimiters

Pass the values directly after the delimiter.

java
public class JoinVarargs {
    public static void main(String[] args) {
        String colors = String.join(", ", "red", "green", "blue");
        String date = String.join("-", "2026", "11", "19");

        System.out.println(colors);
        System.out.println(date);
    }
}

How It Works

  1. Between, not around. ", " appears twice for three colors, with nothing before the first or after the last.
  2. Any delimiter text. A single dash builds an ISO-style date from three parts.
  3. Static call. No string object is needed to start; you write String.join on the class.

Example 2 — Build a Path from a List

The Iterable overload accepts a List directly.

java
import java.util.Arrays;
import java.util.List;

public class JoinList {
    public static void main(String[] args) {
        List<String> folders = Arrays.asList("usr", "local", "bin");

        String path = "/" + String.join("/", folders);
        System.out.println(path);
        System.out.println(folders.size() + " folders, " + path.length() + " chars");
    }
}

How It Works

  1. A fixed-size list. Arrays.asList wraps the three names in a List<String>, which is an Iterable.
  2. Join, then add a prefix. join gives "usr/local/bin"; prepending "/" makes it absolute.
  3. Count the result. 3 + 5 + 3 letters plus 3 slashes make 14 chars.

Example 3 — null Elements, One Element, and No Elements

See how join behaves at the edges.

java
public class JoinEdge {
    public static void main(String[] args) {
        System.out.println(String.join(", ", "a", null, "c"));
        System.out.println(String.join(" + ", "solo"));
        System.out.println("[" + String.join(", ", new String[0]) + "]");
    }
}

How It Works

  1. null becomes text. The missing middle element is written as null, without an exception.
  2. One element, no delimiter. With a single value there is nothing to separate, so only "solo" is returned.
  3. No elements. An empty array produces the empty string, shown as [].

Frequently Asked Questions

It concatenates the given elements and puts the delimiter between each pair, returning a new String.
Java 8.
Call String.join(", ", list). Any Iterable of CharSequence works, including List and Set.
They are added as the text "null". Only a null delimiter or a null collection throws NullPointerException.
Convert them to strings first, for example list.stream().map(String::valueOf).collect(Collectors.joining(", ")).

Next: lastIndexOf()

Find the last position of a char or substring (-1 if absent).

lastIndexOf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful