Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaArrays.toString() in Java with Examples

Arrays.toString() in Java with Examples

Today we are going to discuss the simplest way to print the array as a string in Java: Arrays.toString() method. 

How to use Arrays.toString() method? 

Description: 
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 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. 

Variants:

  • public static String toString(boolean[] arr)
  • public static String toString(byte[] arr)
  • public static String toString(char[] arr)
  • public static String toString(double[] arr)
  • public static String toString(float[] arr)
  • public static String toString(int[] arr)
  • public static String toString(long[] arr)
  • public static String toString(Object[] arr)
  • public static String toString(short[] arr)

Parameters: 
arr – the array whose string representation to return 

Returns: 
the string representation of arr 

Usage: 
The below mentioned Java code depicts the usage of the toString() method of Arrays class with 

examples: 

Java




// Java program to demonstrate working of Arrays.toString()
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        // Let us create different types of arrays and
        // print their contents using Arrays.toString()
        boolean[] boolArr = new boolean[] { true, true, false, true };
        byte[] byteArr = new byte[] { 10, 20, 30 };
        char[] charArr = new char[] { 'g', 'e', 'e', 'k', 's' };
        double[] dblArr = new double[] { 1, 2, 3, 4 };
        float[] floatArr = new float[] { 1, 2, 3, 4 };
        int[] intArr = new int[] { 1, 2, 3, 4 };
        long[] lomgArr = new long[] { 1, 2, 3, 4 };
        Object[] objArr = new Object[] { 1, 2, 3, 4 };
        short[] shortArr = new short[] { 1, 2, 3, 4 };
 
        System.out.println(Arrays.toString(boolArr));
        System.out.println(Arrays.toString(byteArr));
        System.out.println(Arrays.toString(charArr));
        System.out.println(Arrays.toString(dblArr));
        System.out.println(Arrays.toString(floatArr));
        System.out.println(Arrays.toString(intArr));
        System.out.println(Arrays.toString(lomgArr));
        System.out.println(Arrays.toString(objArr));
        System.out.println(Arrays.toString(shortArr));
    }
}


Output

[true, true, false, true]
[10, 20, 30]
[g, e, e, k, s]
[1.0, 2.0, 3.0, 4.0]
[1.0, 2.0, 3.0, 4.0]
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4]

We can also use Arrays.toString() for objects of user defined class. 
Since Arrays.toString() is overloaded for array of Object class (there exist a method Arrays.toString(Object [])) and Object is ancestor of all classes, we can use call it for an array of any type of object. 

Java




// Java program to demonstrate working of Arrays.toString()
// for user defined objects.
import java.lang.*;
import java.util.*;
 
// Driver class
class Main {
    public static void main(String[] args)
    {
        Student[] arr = { new Student(111, "bbbb", "london"),
                        new Student(131, "aaaa", "nyc"),
                        new Student(121, "cccc", "jaipur") };
 
        System.out.println(Arrays.toString(arr));
    }
}
 
// A class to represent a student.
class Student {
    int rollno;
    String name, address;
 
    // Constructor
    public Student(int rollno, String name,
                String address)
    {
        this.rollno = rollno;
        this.name = name;
        this.address = address;
    }
 
    // Used to print student details in main()
    @Override
    public String toString()
    {
        return this.rollno + " " + this.name + " " + this.address;
    }
}
 
// This code is modified by Susobhan Akhuli


Output

[111 bbbb london, 131 aaaa nyc, 121 cccc jaipur]

Why does Object.toString() not work for Arrays?
Using the toString() method on Arrays might not work. It considers an array as a typical object and returns default string, i.e., a ‘[‘ representing an array, followed by a character representing the primitive data type of array followed by an Identity Hex Code [See this for details] 

This article is contributed by Shikhar Goel. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks. 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments