Arrays.toString()
is a static method in Java’s java.util.Arrays
class that returns a string representation of the contents of an array. This method is useful for debugging and logging purposes, as it provides a human-readable format of the array’s elements.
Arrays.toString()
String.valueOf()
.toString()
method of each object.[1, 2, 3]
will be represented as "[1, 2, 3]"
.null
, Arrays.toString()
returns "null"
.null
, it is represented as "null"
in the output string.Here are the main overloaded variants of the Arrays.toString()
method:
public static String toString(boolean[] a)
public static String toString(byte[] a)
public static String toString(char[] a)
public static String toString(double[] a)
public static String toString(float[] a)
public static String toString(int[] a)
public static String toString(long[] a)
public static String toString(Object[] a)
public static String toString(short[] a)
import java.util.Arrays;
public class ArrayToStringExample {
public static void main(String[] args) {
int[] intArray = {1, 2, 3, 4, 5};
double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5};
char[] charArray = {'a', 'b', 'c', 'd', 'e'};
boolean[] booleanArray = {true, false, true, false};
// Using Arrays.toString() on primitive type arrays
System.out.println("intArray: " + Arrays.toString(intArray)); // Output: [1, 2, 3, 4, 5]
System.out.println("doubleArray: " + Arrays.toString(doubleArray)); // Output: [1.1, 2.2, 3.3, 4.4, 5.5]
System.out.println("charArray: " + Arrays.toString(charArray)); // Output: [a, b, c, d, e]
System.out.println("booleanArray: " + Arrays.toString(booleanArray)); // Output: [true, false, true, false]
}
}
import java.util.Arrays;
public class ArrayToStringExample {
public static void main(String[] args) {
String[] stringArray = {"apple", "banana", "cherry"};
Integer[] integerArray = {1, 2, 3, 4, 5};
// Using Arrays.toString() on object type arrays
System.out.println("stringArray: " + Arrays.toString(stringArray)); // Output: [apple, banana, cherry]
System.out.println("integerArray: " + Arrays.toString(integerArray)); // Output: [1, 2, 3, 4, 5]
}
}
For multidimensional arrays, Arrays.toString()
does not produce a fully detailed representation. Instead, it calls toString()
on the nested arrays, resulting in the default object toString()
implementation (which typically outputs a memory address). To properly represent multidimensional arrays, use Arrays.deepToString()
.
import java.util.Arrays;
public class ArrayDeepToStringExample {
public static void main(String[] args) {
int[][] int2DArray = {{1, 2, 3}, {4, 5, 6}};
String[][] string2DArray = {{"apple", "banana"}, {"cherry", "date"}};
// Using Arrays.toString() on multidimensional arrays (not recommended)
System.out.println("int2DArray (toString): " + Arrays.toString(int2DArray));
// Output: [[I@15db9742, [I@6d06d69c]
// Using Arrays.deepToString() on multidimensional arrays (recommended)
System.out.println("int2DArray (deepToString): " + Arrays.deepToString(int2DArray));
// Output: [[1, 2, 3], [4, 5, 6]]
System.out.println("string2DArray (deepToString): " + Arrays.deepToString(string2DArray));
// Output: [[apple, banana], [cherry, date]]
}
}
Arrays.toString()
and Arrays.deepToString()
are very useful for debugging, as they provide a quick way to print the contents of an array.In summary, Arrays.toString()
and Arrays.deepToString()
are valuable tools in Java for generating string representations of arrays, aiding in debugging and logging by providing a clear and readable format of the array contents.