Java Arrays toString()

In this guide, you will learn about the Arrays toString() method in Java programming and how to use it with an example.

1. Arrays toString() Method Overview


The Arrays.toString() method returns a string representation of the contents of the specified array. This method is designed for converting arrays that are not deeply nested. For deeply nested arrays, consider using Arrays.deepToString().


String toString(int[] a)   // Overloaded for other primitive types and Object


- a: The array whose string representation is to be returned.

Key Points:

- This method is overloaded to handle different types of arrays including arrays of objects and arrays of primitive types.

- Returns "null" if the array is null.

- It provides a clear, comma-separated representation of the array's contents, enclosed in square brackets.

- For arrays of objects, it calls the toString method on each element. If the element is null, it prints "null".

2. Arrays toString() Method Example

import java.util.Arrays;

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

        String[] names = {"Alice", "Bob", "Charlie"};

        System.out.println("Numbers array: " + Arrays.toString(numbers));
        System.out.println("Names array: " + Arrays.toString(names));


Numbers array: [1, 2, 3, 4, 5]
Names array: [Alice, Bob, Charlie]


The Arrays.toString() method is utilized to get a string representation of the specified array's contents. In the example, we convert an integer array and a string array to their string representations and print them out. The method provides a clean and readable way to view the contents of an array.