Signup/Sign In

Java Arrays toString() Method

In this tutorial, we will learn toString() method of Arrays class in Java. This method returns a string of the contents of the given array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ",". Each element is converted to strings as by String.valueOf(boolean). This method returns "null" if the given array is null.

Syntax:

static String toString(datatype[] array)

List of Overloading Methods of toString() Method

This table contains all the overloaded variants of toString() method.

Method Description

static String toString(boolean[] array)

This method returns a string representation of the contents of the specified array.

static String toString(byte[] array)

This method returns a string representation of the contents of the specified array.

static String toString(char[] array)

This method returns a string representation of the contents of the specified array.

static String toString(double[] array)

This method returns a string representation of the contents of the specified array.

static String toString(float[] array)

This method returns a string representation of the contents of the specified array.

static String toString(int[] array)

This method returns a string representation of the contents of the specified array.

static String toString(long[] array)

This method returns a string representation of the contents of the specified array.

static String toString(short[] array)

This method returns a string representation of the contents of the specified array.

static String toString(Object[] array)

This method returns a string representation of the contents of the specified array.

Example of toString() method with an array of primitive datatype

In the following example, we have an array of different datatypes i.e. boolean, byte, char, double, float, int, long, Object and short. We are converting all the arrays to string using toString() method.

import java.util.Arrays;
class StudyTonight{ 
	public static void main(String args[]) 
	{  
		boolean[] boolArray = new boolean[] { false, true, false, false }; 
		System.out.println("String of boolean Array: "+Arrays.toString(boolArray));

		byte[] byteArray = new byte[] { 42, 60, 10 };         
		System.out.println("String of byte Array: "+Arrays.toString(byteArray)); 

		char[] charArray = new char[] {'s', 't', 'u', 'd', 'y', 't', 'o', 'n', 'i', 'g', 'h', 't'}; 
		System.out.println("String of char Array: "+Arrays.toString(charArray)); 

		double[] doubleArray = new double[] { 4.4, 1.1, 2.2, 6.6 }; 
		System.out.println("String of double Array: "+Arrays.toString(doubleArray)); 

		float[] floatArray = new float[] { 1f, 3f, 6f, 9f }; 
		System.out.println("String of float Array: "+Arrays.toString(floatArray)); 

		int[] intArray = new int[] { 1, 2, 3, 4 }; 
		System.out.println("String of int Array: "+Arrays.toString(intArray)); 

		long[] longArray = new long[] { 9, 8, 7, 6, 5 }; 
		System.out.println("String of long Array: "+Arrays.toString(longArray)); 

		Object[] objectArray = new Object[] {4, 5, 3, 7, 9}; 
		System.out.println("String of Object Array: "+Arrays.toString(objectArray)); 

		short[] shortArray = new short[] { 1, 2, 3, 4 }; 
		System.out.println("String of short Array: "+Arrays.toString(shortArray)); 
	}
}


String of boolean Array: [false, true, false, false]
String of byte Array: [42, 60, 10]
String of char Array: [s, t, u, d, y, t, o, n, i, g, h, t]
String of double Array: [4.4, 1.1, 2.2, 6.6]
String of float Array: [1.0, 3.0, 6.0, 9.0]
String of int Array: [1, 2, 3, 4]
String of long Array: [9, 8, 7, 6, 5]
String of Object Array: [4, 5, 3, 7, 9]
String of short Array: [1, 2, 3, 4]

Example of toString() method

In the example given below, we are converting the given array of objects of a class to the string and printing it by using toString() method.

import java.util.Arrays;
class Student { 
	int rollno; 
	String name;
	public Student(int rollno, String name) 
	{ 
		this.rollno = rollno; 
		this.name = name; 
	} 
	@Override
	public String toString() 
	{ 
		return this.rollno + " " + this.name;
	} 
} 
class StudyTonight{ 
	public static void main(String args[]) 
	{  
		Student arr[] = new Student[5];
		arr[0]=new Student(1, "AAA");
		arr[1]=new Student(2, "BBB");
		arr[2]=new Student(3, "CCC");
		arr[3]=new Student(4, "DDD");
		arr[4]=new Student(5, "EEE");		 
		System.out.println(Arrays.toString(arr));
	}
}


[1 AAA, 2 BBB, 3 CCC, 4 DDD, 5 EEE]

Conclusion:

In this tutorial, we learned how to implement toString() method on the different types of arrays. We also learned how to convert an array of objects of a class to string and in that case, we need to override to string method.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.