How to print an Array in Java

Syntax:


System.out.println( Arrays.toString( array ) );

The Arrays object from the java.util library formats the content of an Array into a human readable String. That String can be written in the output or the logs.

Example:

This example creates an Array of String containing three elements. The array is converted to a String using Arrays.toString. The output is written in the logs.

import java.util.Arrays;

public class PrintArray {

    public static void main(String[] argv) {

	final String[] tab = {"San Francisco","Los Angeles","New York"};
	
	System.out.println( Arrays.toString( tab ) );
    }

}

The output will be:


[San Francisco, Los Angeles, New York]

If the Array is not converted to a String, the address in memory of the Array is written in the logs.

References:

Java String
Java util.Arrays