W3docs

How to Print an Array in Java

Print Java arrays readably with Arrays.toString, Arrays.deepToString, and stream-based formatting.

How to Print an Array in Java

Passing an array straight to System.out.println almost never prints what you want — you get something like [I@1b6d3586 instead of the elements. Arrays don't override toString(), so the default Object version prints the type and an identity hash. This chapter shows the idiomatic fixes: Arrays.toString for flat arrays, Arrays.deepToString for nested ones, String.join for custom delimiters, and a manual loop when you need full control.

Why a plain print fails

The default toString() an array inherits from Object returns a class signature plus a hash code — not the contents:

int[] numbers = {3, 1, 4};
System.out.println(numbers);       // [I@1b6d3586
System.out.println("" + numbers);  // same reference string

The [I means "array of int"; the hex after @ is the identity hash, which changes between runs. Concatenating the array into a string doesn't help, because concatenation also calls toString(). The fix is to ask a helper to format the elements for you.

Arrays.toString for one dimension

java.util.Arrays.toString walks a single-dimensional array and returns a readable, comma-separated string in square brackets:

int[] numbers = {3, 1, 4, 1, 5};
System.out.println(Arrays.toString(numbers)); // [3, 1, 4, 1, 5]

String[] words = {"alpha", "beta"};
System.out.println(Arrays.toString(words));   // [alpha, beta]

It is overloaded for every primitive array type (int[], double[], boolean[], …) and for Object[], so it works on whatever flat array you hand it. For object arrays it calls each element's own toString().

Arrays.deepToString for nested arrays

Arrays.toString only goes one level deep. On a 2D array it formats the outer array but prints each inner array as a reference. Use Arrays.deepToString to recurse through every level:

int[][] grid = {{1, 2}, {3, 4}};
System.out.println(Arrays.toString(grid));     // [[I@..., [I@...]
System.out.println(Arrays.deepToString(grid)); // [[1, 2], [3, 4]]

Reach for deepToString whenever the array holds other arrays — matrices, jagged arrays, arrays of arrays of objects.

ApproachBest forOutput style
Arrays.toString1D primitive or object arrays[a, b, c]
Arrays.deepToStringnested / multi-dimensional arrays[[a, b], [c, d]]
String.joinString[] with a custom delimitera, b, c (no brackets)
manual loop / StringBuilderfull control over formatanything you build

Custom formatting

When the bracketed default isn't the format you want, String.join joins a String[] (or any CharSequence iterable) with a delimiter of your choice and no surrounding brackets:

String[] words = {"alpha", "beta", "gamma"};
System.out.println(String.join(" | ", words)); // alpha | beta | gamma

For non-string arrays or completely custom output, a loop with a StringBuilder gives you total control over separators, prefixes, and per-element formatting. Streams are another option: Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(", ")).

A runnable example

The program below contrasts the approaches: Arrays.toString on flat int[] and String[], the shallow-vs-deep difference on a 2D array, String.join for a custom delimiter, and a hand-built StringBuilder. The shallow line checks whether the shallow toString of the 2D array still contains inner references, so it prints a stable true instead of a run-varying hash.

java— editable, runs on the server

What to take from the run:

  • toString: [3, 1, 4, 1, 5] shows Arrays.toString turning a flat int[] into readable, bracketed text.
  • words: [alpha, beta, gamma] confirms the same method works on object arrays by calling each element's toString().
  • shallow: true proves that Arrays.toString on a 2D array leaves the inner arrays as [[I@... references, which is why the result starts with [[I@.
  • deep: [[1, 2], [3, 4]] shows Arrays.deepToString recursing into the nested arrays to print every element.
  • joined: alpha | beta | gamma and manual: {3; 1; 4; 1; 5} show String.join and a StringBuilder loop producing bracket-free, custom-delimited output.

Practice

Practice

What does Arrays.toString(grid) print when grid is a two-dimensional int array like {{1, 2}, {3, 4}}?