Java Arrays.deepToString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Arrays

Definition

In Java, Arrays.deepToString(a) returns a String for an array that may contain other arrays. Every inner array is printed with its own brackets, so a 2D array looks like [[1, 2], [3, 4]].

A 2D array in Java is really an array of row arrays. Arrays.toString stops at the first level and prints each row as [I@...; deepToString keeps going into each row, at any depth.

A static method of java.util.Arrays (Java 5+): call it on the class, as in Arrays.deepToString(...); there is no Arrays object to create.

Syntax

Pass any object array; nested arrays inside it are expanded.

Java
int[][] grid = { {1, 2}, {3, 4} };
String s = Arrays.deepToString(grid);   // "[[1, 2], [3, 4]]"
String[][] names = { {"a"}, {} };
Arrays.deepToString(names);             // "[[a], []]"
CallWhat it does
Arrays.deepToString(int[][] grid)Text for a 2D primitive array (it is an Object[] of rows).
Arrays.deepToString(String[][][] cube)Works for any number of dimensions.
Arrays.deepToString(objects)Mixed contents: plain values, arrays, and null.
Arrays.deepToString(null)Returns "null".

There is only one overload, which takes Object[]. A one-dimensional primitive array such as int[] is not an Object[], so deepToString(new int[]{1}) does not compile; use toString for that.

Library declarations
Java
// java.util.Arrays (since Java 5)
public static String deepToString(Object[] a)

Parameters

ParameterDescription
a (Object[])The array to describe. Its elements may themselves be arrays of any type, or null.

Return Value

Return ValueDescription
StringThe nested text, "[]" for an empty array, or "null" when a is null.

Compare with Similar Methods

Choosing the right printing method:

MethodHow it differs
Arrays.toString(a)One level only; correct for int[], String[], and other flat arrays.
Arrays.deepEquals(a, b)The matching comparison method for nested arrays.
Arrays.deepHashCode(a)The matching hash code method for nested arrays.
a loop over rowsPrint one row per line with Arrays.toString(row) for a table-like layout.

Java Examples

Example 1 — Print a 2D Array Two Ways

Print a whole matrix on one line, then print it row by row.

Java
import java.util.Arrays;

public class ArraysDeepToStringDemo {
    public static void main(String[] args) {
        int[][] matrix = { {1, 2, 3}, {4, 5, 6} };
        System.out.println(Arrays.deepToString(matrix));

        for (int[] row : matrix) {
            System.out.println(Arrays.toString(row));
        }
    }
}

How It Works

  1. Rows inside brackets. deepToString prints the outer brackets, then each row with its own brackets: [[1, 2, 3], [4, 5, 6]].
  2. An array of rows. The enhanced for loop shows what matrix really is: two separate int[] objects.
  3. Row by row. Each row is a flat array, so plain Arrays.toString(row) is enough and gives one line per row.
  4. Pick the layout. One line is handy in logs; the loop gives a grid that is easier to read for bigger matrices.

Example 2 — Jagged Rows, Empty Rows, null Rows, and 3D Arrays

Rows of different lengths and a three-dimensional array are all handled.

Java
import java.util.Arrays;

public class ArraysDeepToStringJagged {
    public static void main(String[] args) {
        String[][] seats = { {"A1", "A2", "A3"}, {"B1"}, {}, null };
        System.out.println(Arrays.deepToString(seats));

        int[][][] cube = { { {1, 2}, {3} }, { {4} } };
        System.out.println(Arrays.deepToString(cube));
    }
}

How It Works

  1. Different row lengths. Row A has three seats and row B has one; each row is printed with exactly the elements it has.
  2. Empty versus missing. The third row is an empty array and prints as []; the fourth row is null and prints as the word null.
  3. Three levels deep. For cube, the method goes from the outer array into each 2D block and then into each innermost int[].
  4. Bracket depth shows dimensions. Three opening brackets at the start of the second line mean three levels of nesting.

Example 3 — Mixed Contents and an Array That Contains Itself

An Object[] can hold numbers, characters, arrays, null, and even itself.

Java
import java.util.Arrays;

public class ArraysDeepToStringSelf {
    public static void main(String[] args) {
        Object[] box = new Object[2];
        box[0] = "me";
        box[1] = box;
        System.out.println(Arrays.deepToString(box));

        Object[] mixed = {1, 'c', 2.5, new boolean[] {true}, null};
        System.out.println(Arrays.deepToString(mixed));
        System.out.println(Arrays.deepToString(null));
    }
}

How It Works

  1. A loop in the data. box[1] = box makes the array contain itself, which would recurse forever if printed naively.
  2. Cycle marker. deepToString remembers which arrays it is already printing and writes [...] for the repeat.
  3. Boxed values and a primitive array. In mixed, 1, 'c', and 2.5 are autoboxed objects printed with String.valueOf, while the boolean[] is expanded to [true].
  4. Only one overload. deepToString(null) compiles without a cast because there is a single Object[] version, and it returns null as text.

Frequently Asked Questions

toString prints one level, so the rows of a 2D array show up as [I@... text. deepToString goes into every nested array and prints the values.
Use Arrays.deepToString(matrix) for one line, or loop over the rows and print Arrays.toString(row) for one line per row.
No. An int[] is not an Object[], so the call does not compile. Use Arrays.toString for one-dimensional primitive arrays.
A null row is printed as the word null, while an empty row is printed as [].
It marks an array that contains itself. deepToString prints [...] instead of recursing forever.

Next: Arrays.equals()

Check whether two arrays have the same length and elements.

equals() 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