Question

How to Print a Java Array: Arrays.toString() and Arrays.deepToString()

javaarraysprintingtostring

Question

In Java, printing an array directly does not show its contents in a readable format. Arrays do not override toString(), so printing an array with System.out.println() produces the default Object.toString() output, which looks like the class name plus @ plus the hexadecimal hash code.

For example:

int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray); // Prints something like [I@3343c8b3

Instead, the goal is usually to print the actual values in the array, such as:

[1, 2, 3, 4, 5]

This applies to both primitive arrays and arrays of object references.

Examples:

// Array of primitives:
int[] intArray = new int[] {1, 2, 3, 4, 5};
// Desired output: [1, 2, 3, 4, 5]

// Array of object references:
String[] strArray = new String[] {"John", "Mary", "Bob"};
// Desired output: [John, Mary, Bob]

What is the simplest way to print a Java array in a human-readable format?

Short Answer

By the end of this page, you will understand why Java arrays do not print their contents automatically, how to use Arrays.toString() for normal arrays, and when to use Arrays.deepToString() for nested arrays.

Concept

Java arrays are objects, but they do not provide a custom toString() implementation that lists their elements. That means when you print an array directly, Java falls back to Object.toString().

That default representation typically looks like this:

[I@3343c8b3

This value is not the array contents. It is a low-level object description:

  • [I means "array of int"
  • @ separates the type from the hash code
  • 3343c8b3 is the object's hash code in hexadecimal

To print the actual values, Java provides utility methods in the java.util.Arrays class.

The most common one is:

Arrays.toString(array)

This returns a readable string version of a one-dimensional array.

Example:

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

Output:

[1, 2, 3]

Why this matters in real programming:

  • You often need to inspect data while debugging.
  • Logs should show actual values, not memory-like object descriptions.
  • Test output becomes much easier to read.
  • It helps verify API responses, transformed data, and intermediate program state.

For nested arrays such as int[][], Arrays.toString() is usually not enough. In that case, use:

Arrays.deepToString(array)

That prints nested contents recursively.

Mental Model

Think of an array like a box full of items.

  • Printing the array directly is like reading the box's shipping label.
  • Using Arrays.toString() is like opening the box and listing what's inside.
  • Using Arrays.deepToString() is like opening smaller boxes inside the big box too.

So:

  • System.out.println(array) shows the container identity
  • Arrays.toString(array) shows the items
  • Arrays.deepToString(array) shows items, including inside nested arrays
Take Quiz

Syntax and Examples

The simplest syntax is:

import java.util.Arrays;

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

Example 1: Primitive array

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] intArray = {1, 2, 3, 4, 5};
        System.out.println(Arrays.toString(intArray));
    }
}

Output:

[1, 2, 3, 4, 5]

Arrays.toString() works with primitive arrays such as:

  • int[]
  • double[]
  • char[]
  • boolean[]

Example 2: Object array

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] strArray = {"John", "Mary", "Bob"};
        System.out.println(Arrays.toString(strArray));
    }
}

Output:

[John, Mary, Bob]

Example 3: Nested array

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2},
            {3, 4}
        };

        System.out.println(Arrays.toString(matrix));
        System.out.println(Arrays.deepToString(matrix));
    }
}

Output:

[[I@..., [I@...]
[[1, 2], [3, 4]]

Explanation:

  • Arrays.toString(matrix) prints the inner arrays as objects.
  • Arrays.deepToString(matrix) prints the full nested structure.

Step by Step Execution

Consider this code:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] values = {10, 20, 30};
        String result = Arrays.toString(values);
        System.out.println(result);
    }
}

Here is what happens step by step:

  1. int[] values = {10, 20, 30};

    • Java creates an integer array with three elements.
    • The array holds 10, 20, and 30.
  2. String result = Arrays.toString(values);

    • The Arrays utility class reads each element in the array.
    • It builds a new string in this format:
      [10, 20, 30]
      
    • That string is stored in result.
  3. System.out.println(result);

    • Java prints the string exactly as text.

Final output:

[10, 20, 30]

Now compare it with this:

System.out.println(values);

That does not convert the array contents into readable text. It prints the array object's default string form instead.

Real World Use Cases

Readable array printing is useful in many practical situations:

Debugging program state

int[] scores = {85, 90, 78};
System.out.println(Arrays.toString(scores));

You can quickly verify that your logic produced the expected values.

Logging API or user input data

String[] tags = {"java", "arrays", "debugging"};
System.out.println("Tags: " + Arrays.toString(tags));

This makes logs understandable.

Testing and assertions

When a test fails, readable array output helps compare expected and actual values.

System.out.println("Expected: [1, 2, 3]");
System.out.println("Actual:   " + Arrays.toString(actual));

Data processing scripts

If you transform arrays in a batch job, printing results after each step helps validate the pipeline.

Working with matrices or grid data

For two-dimensional arrays, Arrays.deepToString() is useful when inspecting table-like data such as game boards, seating layouts, or numeric matrices.

Take Quiz

Real Codebase Usage

In real projects, developers usually do not print arrays directly. They use utility methods to make logs and debugging output readable.

Common patterns include:

Logging computed values

import java.util.Arrays;

int[] filtered = {2, 4, 6};
System.out.println("Filtered values: " + Arrays.toString(filtered));

Guarding against null

Arrays.toString(null) is safe and returns the string "null", but teams often still make the intent explicit.

import java.util.Arrays;

String[] names = null;
System.out.println(names == null ? "No names provided" : Arrays.toString(names));

Printing nested configuration data

import java.util.Arrays;

String[][] roles = {
    {"admin", "editor"},
    {"viewer"}
};

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

Combining with validation

import java.util.Arrays;

int[] values = {1, 2, 3};
if (values.length == 0) {
    System.out.println("Array is empty");
    return;
}

System.out.println(Arrays.toString(values));

Using streams when custom formatting is needed

Sometimes developers want a different output format than [a, b, c].

import java.util.Arrays;
import java.util.stream.Collectors;

String[] names = {"John", "Mary", "Bob"};
String csv = Arrays.stream(names).collect(Collectors.joining(", "));
System.out.println(csv);

Output:

John, Mary, Bob

This is useful when you need logs, CSV lines, or UI-friendly output.

Common Mistakes

1. Printing the array directly

Broken code:

int[] numbers = {1, 2, 3};
System.out.println(numbers);

Problem:

  • This prints something like [I@1a2b3c4d.
  • It shows object information, not the contents.

Fix:

import java.util.Arrays;

System.out.println(Arrays.toString(numbers));

2. Forgetting to import Arrays

Broken code:

System.out.println(Arrays.toString(numbers));

Problem:

  • Without import java.util.Arrays;, the code will not compile unless you use the fully qualified name.

Fix:

import java.util.Arrays;

Or:

System.out.println(java.util.Arrays.toString(numbers));

3. Using Arrays.toString() for nested arrays

Broken code:

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

Problem:

  • The inner arrays still print as object references.

Fix:

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

4. Assuming arrays behave like collections

Beginners sometimes expect arrays to print like ArrayList.

import java.util.ArrayList;

ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
System.out.println(list); // [1, 2]

Arrays do not behave the same way because they do not override toString().

5. Writing manual loops when built-in methods are enough

This works, but is often unnecessary for simple output:

int[] numbers = {1, 2, 3};
System.out.print("[");
for (int i = 0; i < numbers.length; i++) {
    System.out.print(numbers[i]);
    if (i < numbers.length - 1) {
        System.out.print(", ");
    }
}
System.out.println("]");

Use this only if you need custom formatting. Otherwise, prefer Arrays.toString() for simplicity and readability.

Comparisons

ApproachBest forOutput styleHandles nested arrays well?Notes
System.out.println(array)Almost never for readable outputObject identity like [I@3343c8b3NoDefault Object.toString()
Arrays.toString(array)One-dimensional arrays[1, 2, 3]NoSimplest choice for most arrays
Arrays.deepToString(array)Nested arrays like int[][] or String[][][[1, 2], [3, 4]]YesRecursively prints inner arrays
Manual loopCustom formattingAnything you wantYes, with extra codeMore control, more code
Streams + joiningCustom string output, usually object arraysa, b, cNot automaticallyGood for special formatting

Arrays.toString() vs Arrays.deepToString()

  • Use Arrays.toString() for normal one-dimensional arrays.
  • Use Arrays.deepToString() when the array contains other arrays.

Arrays vs collections

TypeDirect println() readable?Example output
int[]No[I@3343c8b3
String[]No[Ljava.lang.String;@...
ArrayList<String>Yes[John, Mary, Bob]

Collections such as ArrayList override toString(), but arrays do not.

Take Quiz

Cheat Sheet

import java.util.Arrays;

Print a one-dimensional array

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

Examples:

int[] a = {1, 2, 3};
System.out.println(Arrays.toString(a)); // [1, 2, 3]

String[] names = {"John", "Mary"};
System.out.println(Arrays.toString(names)); // [John, Mary]

Print a nested array

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

Example:

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

Avoid this for readable output

System.out.println(array);

Quick rules

  • Arrays.toString() → one-dimensional arrays
  • Arrays.deepToString() → nested arrays
  • Arrays do not override toString()
  • Collections like ArrayList do print contents directly
  • Use manual loops only for custom formatting

Edge case

int[] values = null;
System.out.println(Arrays.toString(values)); // null

That prints the string null, not an exception.

FAQ

Why does Java print [I@... for an int[]?

Because arrays use the default Object.toString() output instead of showing their elements.

What is the simplest way to print a Java array?

Use Arrays.toString(array) for a normal one-dimensional array.

How do I print a 2D array in Java?

Use Arrays.deepToString(array) for arrays that contain other arrays.

Do I need to import anything to print arrays nicely?

Yes. Usually you import:

import java.util.Arrays;

Does Arrays.toString() work for both primitive and object arrays?

Yes. It works for arrays like int[], double[], and String[].

Can I print an array without Arrays.toString()?

Yes, by writing a loop yourself, but that is usually more code and only needed for custom formatting.

Why does ArrayList print nicely but arrays do not?

ArrayList overrides toString(), while arrays do not.

Is Arrays.toString(null) safe?

Yes. It returns the string null.

Take Quiz

Mini Project

Description

Build a small Java program that prints different kinds of arrays in a readable way. This project helps you practice choosing between Arrays.toString() and Arrays.deepToString() and shows how array output changes depending on the array structure.

Goal

Create a Java program that prints a primitive array, an object array, and a nested array using the correct array-printing method.

Requirements

  • Create one primitive array such as int[].
  • Create one object array such as String[].
  • Create one nested array such as int[][].
  • Print the one-dimensional arrays with Arrays.toString().
  • Print the nested array with Arrays.deepToString().
Take Quiz

Keep learning

Related questions

View more