Arrays ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-07-13

Arrays.deepToString() Method in Java

The Arrays.deepToString() method in Java returns a string representation of a multidimensional array: it recursively walks through the nested arrays and wraps every level in square brackets. It is the shortest way to print a 2D or multidimensional array to the console without writing loops. The method is declared in the java.util.Arrays class:

public static String deepToString(Object[] a)

Example: converting a 2D array to a string

Pass a two-dimensional String array to the method — Arrays.deepToString() returns the contents of both nesting levels:

import java.util.Arrays;

public class ArraysDeepToStringExample {
    public static void main(String[] args) {
        String[][] array = {{"one-one", "one-two", "one-three"},
                {"two-one", "two-two", "two-three"}};
        System.out.println(Arrays.deepToString(array));
    }
}

Program output:

[[one-one, one-two, one-three], [two-one, two-two, two-three]]

The outer brackets represent the whole array, the inner ones represent each of its rows. The method works with any nesting depth: a three-dimensional array produces three levels of brackets, and so on. That makes it handy for debugging and for printing tables, matrices, and other nested structures.

Difference from Arrays.toString()

The Arrays class has a similar method — Arrays.toString(). It processes only the first level of the array: each element is converted by calling its own toString(). For a 2D array the first-level elements are the nested arrays themselves, and arrays do not override toString() — so instead of the contents you get the type and hash code:

String[][] array = {{"one-one", "one-two"}, {"two-one", "two-two"}};

System.out.println(Arrays.toString(array));
System.out.println(Arrays.deepToString(array));

Program output:

[[Ljava.lang.String;@1b6d3586, [Ljava.lang.String;@4554617c]
[[one-one, one-two], [two-one, two-two]]
Method Accepts How it traverses When to use
Arrays.toString() One-dimensional array (primitives or objects) First level only Printing a one-dimensional array
Arrays.deepToString() Object[] (including int[][], String[][]) Recursively, all nesting levels Printing 2D and multidimensional arrays

Arrays of primitives

A two-dimensional array of primitives, such as int[][], can be passed to deepToString(): its first-level elements are objects of type int[], so the array is compatible with the Object[] parameter:

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

Program output:

[[1, 2, 3], [4, 5, 6]]

Important

A one-dimensional array of primitives cannot be passed to deepToString(): Arrays.deepToString(new int[]{1, 2, 3}) does not compile, because int[] is not convertible to Object[]. For one-dimensional arrays use the Arrays.toString() overloads — they exist for every primitive type.

null and self-referencing arrays

The method handles the edge cases that usually break a hand-written recursive printer:

  • if the argument is null, the string "null" is returned;
  • nested null elements are also printed as null;
  • if the array directly or indirectly contains a reference to itself, "[...]" is printed in that place instead of infinite recursion.
Object[] selfRef = new Object[2];
selfRef[0] = "first";
selfRef[1] = selfRef; // the array references itself

System.out.println(Arrays.deepToString(selfRef));

Program output:

[first, [...]]

For comparing and hashing multidimensional arrays, the Arrays class offers companion methods with the same “deep” semantics — Arrays.deepEquals() and Arrays.deepHashCode().

Frequently Asked Questions

What is the difference between Arrays.deepToString() and Arrays.toString()?

Arrays.toString() processes only the first level of the array, so for a 2D array it prints the type and hash code of the nested arrays. Arrays.deepToString() traverses the array recursively and prints the contents of every nesting level.

Can I pass a one-dimensional int[] array to deepToString()?

No, the code will not compile: the method accepts Object[], and a primitive array int[] is not convertible to that type. For a one-dimensional primitive array use Arrays.toString(). An int[][] array, however, is accepted — its first-level elements are objects themselves.

How do I print a 2D array in Java without deepToString()?

With nested for loops: the outer loop goes over the rows, the inner one over the elements of each row. Or with a single loop over the rows, printing each via Arrays.toString(row) — that way every row of the matrix appears on its own line, which is more readable for large tables.

What does deepToString() return if the array contains null or references itself?

For a null argument the method returns the string "null", and nested null elements are printed as null. If the array contains a reference to itself, "[...]" is printed in that place instead of infinite recursion.

Comments

Please log in or register to have a possibility to add comment.