Difference Between Arrays.toString() and Arrays.deepToString() in Java
Last Updated :
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.
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
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));
}
}
|
Output
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
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));
}
}
|
Output
Using Arrays.toString(): [1, 2, 3, 4, 5]
3. Multi-Dimension Array:
Java
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));
}
}
|
Output
Using Arrays.toString(): [[Ljava.lang.Integer;@3d075dc0, [Ljava.lang.Integer;@214c265e]
Using Arrays.deepToString(): [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
Similar Reads
Difference between Array and String in Java
An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value
5 min read
Difference between String and Character array in Java
Unlike C/C++ Character arrays and Strings are two different things in Java. Both Character Arrays and Strings are a collection of characters but are different in terms of properties. Differences between Strings and Character Arrays: PropertyStringCharacter ArrayDefinitionA sequence of characters is
3 min read
Difference between length of Array and size of ArrayList in Java
Array and ArrayList are two different Entities in Java. In this article we will learn the difference between length of Array and size of ArrayList in Java. Array has length property which provides the length of the Array or Array object. It is the total space allocated in memory during the initializ
2 min read
Difference between List and ArrayList in Java
A Collection is a group of individual objects represented as a single unit. Java provides a Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit This framework consists of the List Interface as well as the ArrayList class. In this article
4 min read
Difference Between charAt() and substring() Method in Java
In Java, the charAt() method of the String class is used to extract the character from a string. It returns the character at the specified index in the String. The substring() method is used to extract some portion from the actual String and the actual string remains the same as it is. After that, t
3 min read
Difference between Stream.of() and Arrays.stream() method in Java
Arrays.stream() The stream(T[] array) method of Arrays class in Java, is used to get a Sequential Stream from the array passed as the parameter with its elements. It returns a sequential Stream with the elements of the array, passed as parameter, as its source. Example: Java Code // Java program to
5 min read
Difference Between Length and Capacity in Java
Length and capacity are two different things in Java. Length basically increases when characters are appended to the string whereas the capacity increases when the current capacity is exceeded by a new length. length () method is a part of the Java String class. It is used to find the length of the
3 min read
What is the difference between lists and arrays?
In programming, lists and arrays are data structures used to organize and store data. Both have their unique features and purposes. Lists are dynamic and flexible, allowing for easy resizing during runtime, while arrays are static with a fixed size. This difference impacts memory usage and performan
8 min read
Difference between List, Set and Map in Java
List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows pos
4 min read
Is there any difference between int[] a and int a[] in Java?
An array in Java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. In Java, Array can be declared in the following ways: One-Dimensional Arrays: The general form of a one-dimensional array declaration is type var-name[];ORtype[] v
6 min read