Skip to main content

How to Print a List in Java

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

System.out.println(list) prints a Java list's elements between square brackets, separated by a comma and a space: [customer, beta-tester]. The same text appears when you join a list to a string with +, because both use the list's toString.

Syntax​

Syntax
System.out.println(list);
System.out.println("text" + list);
PartWhat it does
System.out.println(list)Prints the list's toString: its elements between square brackets, separated by , .
"text" + listJoins the text and the list's toString into one string.
listAny list, such as one made with List.of.

The format comes from toString, which the lists in this post inherit from AbstractCollection: "The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object)."

The Example​

The example prints a list of strings, the same list after some text, a list of numbers and an empty list.

Prerequisites​

  • A JDK 11 or later, which can run a single .java file directly. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which creates a list of the roles customer and beta-tester and a list of the numbers 10 and 500, then prints the roles, the roles after the text roles:, the numbers and an empty list.

Main.java
import java.util.List;

public class Main {

public static void main(String[] args) {
List<String> roles = List.of("customer", "beta-tester");
List<Integer> limits = List.of(10, 500);

System.out.println(roles);
System.out.println("roles: " + roles);
System.out.println(limits);
System.out.println(List.of());
}
}

Step 2: Run It​

Terminal
java Main.java
Output
[customer, beta-tester]
roles: [customer, beta-tester]
[10, 500]
[]

A zsh terminal where java Main.java prints customer, beta-tester in square brackets, the same after roles:, then 10, 500 in square brackets and an empty pair of brackets.

java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.

What Happened​

  • println(roles) printed [customer, beta-tester]: the elements in order, between square brackets, with , between them. The strings print without quotes.
  • "roles: " + roles printed the same list after roles: . Joining a list to a string with + uses the same toString.
  • println(limits) printed [10, 500]. Numbers print the same way.
  • println(List.of()) printed [], an empty pair of brackets.

Common Mistakes​

MistakeWhat happensDo this instead
Printing an array instead of a listSomething like [Ljava.lang.String;@1cab0bfb: the array's type and a number, not its elementsUse Arrays.toString(array), which printed [customer, beta-tester]
Printing a list of objects whose class has no toStringSomething like [Main$Plan@76329302]Give the class a toString, or make it a record: a record printed [Plan[name=pro, limit=500]]
Using String.join on a list of numberserror: no suitable method found for join(String,List<Integer>)Use String.join for strings only, and print other lists as they are

Troubleshooting​

SymptomLikely causeFix
[Ljava.lang.String;@ followed by letters and digitsAn array was printed, not a listUse Arrays.toString
A class name, @ and a number inside the brackets, such as Main$Plan@76329302The elements' class doesn't have its own toStringAdd a toString, or make the class a record
error: no suitable method found for join(String,List<Integer>)String.join was given a list that isn't textPrint the list with println

FAQ​

How do I print a list without the brackets?​

For a list of strings, use String.join: String.join(", ", roles) printed customer, beta-tester.

How are null and nested lists printed?​

Each element prints as its own toString, and null as the word null. A list holding "a", null, a list of 1 and 2, and a map printed [a, null, [1, 2], {k=v}].

Conclusion​

System.out.println(list) prints a list's elements between square brackets, separated by commas, and "text" + list gives the same text after your own. Arrays and classes without a toString don't print that way, so use Arrays.toString for an array, and give your own classes a toString.

Additional Resources​