Open In App
Related Articles

Arrays.toString() in Java with Examples

Improve Article
Save Article
Like Article

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? 

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. 


  • 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)

arr – the array whose string representation to return 

the string representation of arr 

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



// Java program to demonstrate working of Arrays.toString()
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 };


[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 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") };
// 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; = name;
        this.address = address;
    // Used to print student details in main()
    public String toString()
        return this.rollno + " " + + " " + this.address;
// This code is modified by Susobhan Akhuli


[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] 

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks 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.

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 16 Feb, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials