In Java, Arrays.toString(a) returns a String that lists the elements of a one-dimensional array in square brackets, separated by a comma and a space: [1, 2, 3].
You need it because arrays do not override toString(). System.out.println(nums) prints a type code and a hash such as [I@1b6d3586, which changes from run to run and tells you nothing about the contents. Arrays.toString prints the values instead.
A static method of java.util.Arrays (Java 5+): call it on the class, as in Arrays.toString(...); there is no Arrays object to create.
Text for an int[]; the same overload exists for every primitive type.
Arrays.toString(Object[] a)
Uses String.valueOf(element) for each element.
Arrays.toString(emptyArray)
Returns "[]".
Arrays.toString(null)
Returns "null" (cast the null so the compiler can pick an overload).
There are nine overloads: long[], int[], short[], char[], byte[], boolean[], float[], double[], and Object[]. Each one formats the elements the same way String.valueOf would.
Library declarations
Java
// java.util.Arrays (since Java 5)
public static String toString(int[] a)
public static String toString(long[] a)
public static String toString(double[] a)
public static String toString(char[] a)
public static String toString(boolean[] a)
public static String toString(Object[] a)
// also short[], byte[], float[]
Parameters
Parameter
Description
a
The array to describe. May be null or empty.
Return Value
Return Value
Description
String
"[e0, e1, ...]", "[]" for an empty array, or "null" when a is null.
One overload per type. Each call picks the version that matches its argument: toString(int[]), toString(double[]), and so on.
Doubles keep a decimal point. 10.0 prints as 10.0, exactly as String.valueOf(10.0) would write it.
Characters without quotes. The char[] shows A, B, A; the single quotes from the source code are not part of the text.
Same brackets everywhere. All four results use square brackets and a comma plus a space between elements.
Example 2 — Empty Arrays, null Arrays, and null Elements
See what toString returns for the edge cases that often show up in real data.
Java
import java.util.Arrays;
public class ArraysToStringEdgeCases {
public static void main(String[] args) {
int[] empty = {};
int[] missing = null;
String[] words = {"hi", null, ""};
System.out.println("empty: " + Arrays.toString(empty));
System.out.println("null: " + Arrays.toString(missing));
System.out.println("strings: " + Arrays.toString(words));
}
}
Output
empty: []
null: null
strings: [hi, null, ]
How It Works
Zero elements. An array of length 0 becomes a pair of brackets with nothing inside.
A null array is safe. Because missing is typed int[], the compiler picks toString(int[]), which returns the word null instead of throwing.
A null element. The second string is null, and String.valueOf writes it as null.
An empty string is invisible. The last element is "", so nothing appears after the final comma and space.
Example 3 — Arrays of Your Own Objects
Give a class a toString() method and Arrays.toString uses it for every element.
Java
import java.util.Arrays;
public class ArraysToStringObjects {
static class Item {
final String name;
final int qty;
Item(String name, int qty) { this.name = name; this.qty = qty; }
@Override
public String toString() { return name + " x" + qty; }
}
public static void main(String[] args) {
Item[] cart = {new Item("pen", 3), new Item("pad", 1)};
System.out.println(Arrays.toString(cart));
char[] letters = {'o', 'k'};
System.out.println(Arrays.toString(letters) + " vs " + new String(letters));
}
}
Output
[pen x3, pad x1]
[o, k] vs ok
How It Works
Object arrays.cart is an Item[], so the toString(Object[]) overload runs.
Your method is called. For each element it calls String.valueOf(item), which calls the overridden toString() and gives pen x3 and pad x1.
Without the override. If Item did not override toString(), each element would print as the class name plus a hash.
Two views of a char array.Arrays.toString(letters) lists the characters as [o, k], while new String(letters) joins them into the word ok.
Frequently Asked Questions
Arrays inherit toString() from Object, which prints a type code ([I means int array) followed by a hash. Use Arrays.toString(array) to print the elements.
An empty array gives "[]" and a null array gives "null". It never throws an exception.
Only one level deep, so each row prints as [I@... text. Use Arrays.deepToString for int[][] and other nested arrays.
Use new String(chars) or String.valueOf(chars). Arrays.toString(chars) prints the characters separated by commas inside brackets.
Not with Arrays.toString. Build the text yourself with a loop or StringBuilder, or use String.join for a String[], or a stream with Collectors.joining.