Arrays.toString() Method in Java
The Arrays.toString() method returns a string representation of a one-dimensional array in Java: the elements are listed comma-separated and enclosed in square brackets. It is the shortest way to print an array to the console — one call instead of looping over the elements with a for loop. The method is static and lives in the java.util.Arrays class, so you need the import java.util.Arrays; at the top of the file.
How Arrays.toString() works
Pass a one-dimensional array to the method — and you get a ready-to-use string:
import java.util.Arrays;
public class ArraysToStringExample {
public static void main(String[] args) {
int[] array = {1, 4, 6, 3, 8};
System.out.println(Arrays.toString(array));
}
} Console output:
[1, 4, 6, 3, 8] The method works with arrays of any type. For a String[] array each element goes into the result as is, without quotes:
String[] names = {"Anna", "Boris", "Clara"};
System.out.println(Arrays.toString(names)); // [Anna, Boris, Clara] A full description of all overloads is available in the official Oracle documentation for the Arrays class.
Note
Arrays.toString() has nine overloads: one for each of the eight primitive types (int[], long[], double[], char[], etc.) and one for Object[]. That is why the method accepts an array of any type — no conversion is needed on your side.
Why an array prints as [I@1b6d3586
If you pass an array directly to System.out.println(), instead of the elements you will see something like [I@1b6d3586 on the console:
int[] array = {1, 4, 6, 3, 8};
System.out.println(array); // [I@1b6d3586
System.out.println(Arrays.toString(array)); // [1, 4, 6, 3, 8] The reason is that an array in Java is an object, but it does not override the toString() method. The default implementation from the Object class kicks in: the type name ([I — a one-dimensional int array), the @ character, and the hash code in hexadecimal. Such a string carries no information about the elements — which is exactly why Arrays.toString() exists. With a two-dimensional array the same problem repeats at every nesting level:
int[][] twoDArray = {{1, 2}, {3, 4}};
System.out.println(twoDArray); // [[I@4554617c
System.out.println(Arrays.toString(twoDArray)); // [[I@74a14482, [I@1540e19d] For such cases there is a dedicated method Arrays.deepToString(), covered in detail in the lesson «Arrays.deepToString() method».
Comparing ways to print an array
| Approach | Works for | Result |
|---|---|---|
| System.out.println(array) | Any array (except char[]) | Type and hash code: [I@1b6d3586 |
| Arrays.toString(array) | One-dimensional array of any type | [1, 4, 6, 3, 8] |
| Arrays.deepToString(array) | Multidimensional (Object[]) | [[1, 2, 3], [4, 5, 6]] |
| String.join(", ", array) | String[] and CharSequence[] | Anna, Boris, Clara — no brackets |
How to convert an array to a string without brackets
Arrays.toString() always adds square brackets and commas. If you need to convert an array to a string in your own format — for example, to write it to a file or build a message — use String.join() for a string array:
String[] names = {"Anna", "Boris", "Clara"};
String result = String.join(", ", names);
System.out.println(result); // Anna, Boris, Clara For an array of primitives, the Stream API with Collectors.joining() does the job:
int[] array = {1, 4, 6, 3, 8};
String result = Arrays.stream(array)
.mapToObj(String::valueOf)
.collect(Collectors.joining(", "));
System.out.println(result); // 1, 4, 6, 3, 8 The separator can be anything in both cases: a hyphen, a line break, a semicolon.
Frequently Asked Questions
What does Arrays.toString() return if you pass null?
The string "null" — no exception is thrown. This is handy during debugging: the method safely prints uninitialized array references as well.
Why does System.out.println(charArray) print characters instead of a hash code?
println() has a separate overload for char[] that prints the array contents as text. It is the only array type with this behavior — all others are printed as type and hash code. Arrays.toString(charArray), in turn, prints the characters comma-separated: [h, e, l, l, o].
How do I print an array of my own class objects?
Arrays.toString() calls toString() on every element. If your class does not override toString(), instead of the object fields you will see a list like ClassName@hash-code. Override toString() in your class — and the output becomes readable.
Can I compare arrays via Arrays.toString()?
Technically you can compare the resulting strings, but it is extra work and a source of subtle bugs. For comparing array contents Java provides Arrays.equals() (one-dimensional) and Arrays.deepEquals() (multidimensional).
Comments