Question
How to Print a Java Array: Arrays.toString() and Arrays.deepToString()
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:
[Imeans "array of int"@separates the type from the hash code3343c8b3is 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, , ]
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 identityArrays.toString(array)shows the itemsArrays.deepToString(array)shows items, including inside nested arrays
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
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:
-
int[] values = {10, 20, 30};- Java creates an integer array with three elements.
- The array holds
10,20, and30.
-
String result = Arrays.toString(values);- The
Arraysutility class reads each element in the array. - It builds a new string in this format:
[10, 20, ]
- The
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.
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;
[] values = {, , };
(values.length == ) {
System.out.println();
;
}
System.out.println(Arrays.toString(values));
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
Comparisons
| Approach | Best for | Output style | Handles nested arrays well? | Notes |
|---|---|---|---|---|
System.out.println(array) | Almost never for readable output | Object identity like [I@3343c8b3 | No | Default Object.toString() |
Arrays.toString(array) | One-dimensional arrays | [1, 2, 3] | No | Simplest choice for most arrays |
Arrays.deepToString(array) | Nested arrays like int[][] or String[][] | [[1, 2], [3, 4]] |
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
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?
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().
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.