Difference Between Arrays.toString() and Arrays.deepToString() in Java - GeeksforGeeks

28 Jan, 2021
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.
Arrays.toString()
Arrays.deepToString()
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]]

Java-Arrays
Picked
Technical Scripter 2020
Difference Between
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Informed and Uninformed Search in AI
Difference between HashMap and HashSet
Difference between Internal and External fragmentation
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples