The deepToString() method of the Arrays class returns the string representation of the deep contents of the specified Object array. Unlike Arrays. toString(), if the array contains other arrays as elements, the string representation includes their contents, and so on.
Arrays.toString(): Returns a string representation of the contents of the specified array. The string representation consists of a list of the array’s elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters “, ” (a comma followed by a space). Returns “null” if a is null.
In the case of an Object Array, if the array contains other arrays as elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.
- Parameters: The array whose string representation to return.
- Returns: The string representation of the array.
Arrays.deepToString(): java.util.Arrays.deepToString(Object[]) method is a java.util.Arrays class method. Returns a string representation of the “deep contents” of the specified array. If the array contains other arrays as elements, the string representation contains their contents, and so on. This method is designed for converting multidimensional arrays to strings. The simple toString() method works well for simple arrays but doesn’t work for multidimensional arrays. This method is designed for converting multidimensional arrays to strings.
Syntax:
public static String deepToString(Object[] arr)
arr – An array whose string representation is needed
This function returns string representation of arr[]. It returns “null” if the specified array is null.
Difference Between Arrays.toString() and Arrays.toDeepString() in Java
Arrays.toString() |
Arrays.deepToString() |
---|---|
We use Arrays.toString() in java to get the String representation of Object | We use Arrays.deepToString() method to get String Representation of Deep Content of the Specific array. |
We widely use Arrays.toString() to return String() representation of single dimension Array. | We can use Arrays.deepToString() method to return String representation of both single dimension and multi-dimension Arrays. |
If the array contains other arrays as elements, the string representation does not contain their contents, and so on. | If the array contains other arrays as elements, the string representation contains their contents, and so on. |
We do not use Arrays.toString() method to return String representation of multi-Dimension Array. | We can use Arrays.deepToString() method to return String representation of multi-dimension Arrays. |
Arrays.toString() work on arrays of primitives. | Arrays.deepToString() does not work on Primitives. |
Below is illustration of Arrays.toString() and Arrays.deepToString():
1. Single dimension array of Integer.
Java
// Java program to show the usage of // Arrays.toString() and Arrays.deepToString() import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { Integer[] a = { 1 , 2 , 3 , 4 , 5 }; System.out.println( "Using Arrays.toString(): " + Arrays.toString(a)); System.out.println( "Using Arrays.deepToString(): " + Arrays.deepToString(a)); } } |
Using Arrays.toString(): [1, 2, 3, 4, 5] Using Arrays.deepToString(): [1, 2, 3, 4, 5]
2. Single dimension Array of Primitive(int) in which only Arrays.toString() work.
Java
// Java program to show the usage of // Arrays.toString() and Arrays.deepToString() import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { int [] a = { 1 , 2 , 3 , 4 , 5 }; System.out.println( "Using Arrays.toString(): " + Arrays.toString(a)); } } |
Using Arrays.toString(): [1, 2, 3, 4, 5]
3. Multi-Dimension Array:
Java
// Java program to show the usage of // Arrays.toString() and Arrays.deepToString() import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { Integer[] a1 = { 1 , 2 , 3 , 4 , 5 }; Integer[] a2 = { 6 , 7 , 8 , 9 , 10 }; Integer[][] multi = { a1, a2 }; System.out.println( "Using Arrays.toString(): " + Arrays.toString(multi)); System.out.println( "Using Arrays.deepToString(): " + Arrays.deepToString(multi)); } } |
Using Arrays.toString(): [[Ljava.lang.Integer;@3d075dc0, [Ljava.lang.Integer;@214c265e] Using Arrays.deepToString(): [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]