Java Arrays.toString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Arrays

Definition

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.

Syntax

Pass the array; print or store the returned text.

Java
int[] nums = {1, 2, 3};
String s = Arrays.toString(nums);     // "[1, 2, 3]"
Arrays.toString(new int[0]);          // "[]"
Arrays.toString((int[]) null);        // "null"
CallWhat it does
Arrays.toString(int[] a)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

ParameterDescription
aThe array to describe. May be null or empty.

Return Value

Return ValueDescription
String"[e0, e1, ...]", "[]" for an empty array, or "null" when a is null.

Compare with Similar Methods

Other ways to turn an array into text:

MethodHow it differs
Arrays.deepToString(a)Prints nested arrays such as int[][] all the way down.
System.out.println(a)Prints a type code and hash such as [I@1b6d3586, never the elements.
new String(chars)Joins a char[] into one word with no brackets or commas.
Arrays.stream(a)Lets you build custom text, for example with Collectors.joining(" | ").

Java Examples

Example 1 — Print Arrays of Different Types

Print an int[], a double[], a char[], and a boolean[].

Java
import java.util.Arrays;

public class ArraysToStringDemo {
    public static void main(String[] args) {
        int[] ids = {101, 102, 103};
        double[] prices = {2.5, 10.0};
        char[] grades = {'A', 'B', 'A'};
        boolean[] flags = {true, false};

        System.out.println(Arrays.toString(ids));
        System.out.println(Arrays.toString(prices));
        System.out.println(Arrays.toString(grades));
        System.out.println(Arrays.toString(flags));
    }
}

How It Works

  1. One overload per type. Each call picks the version that matches its argument: toString(int[]), toString(double[]), and so on.
  2. Doubles keep a decimal point. 10.0 prints as 10.0, exactly as String.valueOf(10.0) would write it.
  3. Characters without quotes. The char[] shows A, B, A; the single quotes from the source code are not part of the text.
  4. 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));
    }
}

How It Works

  1. Zero elements. An array of length 0 becomes a pair of brackets with nothing inside.
  2. A null array is safe. Because missing is typed int[], the compiler picks toString(int[]), which returns the word null instead of throwing.
  3. A null element. The second string is null, and String.valueOf writes it as null.
  4. 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));
    }
}

How It Works

  1. Object arrays. cart is an Item[], so the toString(Object[]) overload runs.
  2. Your method is called. For each element it calls String.valueOf(item), which calls the overridden toString() and gives pen x3 and pad x1.
  3. Without the override. If Item did not override toString(), each element would print as the class name plus a hash.
  4. 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.

Back to the method index

See all 21 Arrays method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful