Array toString Java

Array toString Java: Welcome to the tutorial on the Arrays class’s toString() method in Java. This method returns a string representation of the given array. The returned string will list all array elements, separated by commas and enclosed in square brackets. Any element not already a string will be converted to one by String.valueOf(boolean). Finally, this method returns “null” if you input a null array.

Array and String are two of the most important data structures for programmers. They are closely related, not just because String is a character array in most programming languages but also because of popularity.

Unfortunately, there is no direct way of converting arrays to Strings in Java. But If you attempt to convert an Integer array to a String, you will receive something like I@4fee225 due to the default implementation of the toString() method from the java.lang.Object class.

Post Type:Core Java Tutorial
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

Array toString Java Syntax:

static String toString(datatype[] array)

List of Overloading Methods of toString() Method

Below, we have tried to share all the different variants of the toString() Method:

public static String toString(boolean[] arr)This method returns a string representation of the contents of an array.
public static String toString(byte[] arr)This method returns a string representation of the contents of an array.
public static String toString(char[] arr)This method returns a string representation of the contents of an array.
public static String toString(double[] arr)This method returns a string representation of the contents of an array.
public static String toString(float[] arr)This method returns a string representation of the contents of an array.
public static String toString(int[] arr)This method returns a string representation of the contents of an array.
public static String toString(long[] arr)This method returns a string representation of the contents of an array.
public static String toString(Object[] arr)This method returns a string representation of the contents of an array.
public static String toString(short[] arr)This method returns a string representation of the contents of an array.

Array toString Java Example

We can have various data types: boolean, byte, char, double, float, int long, and short. Using the toString() method, we can convert all the arrays to a string.

package com.softwaretestingo.java.basics;
import java.util.Arrays;
public class ArraytoString 
{
   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));
   }
}
package myPackage;
public class Test 
{
	public static void main(String[] args) 
	{
		boolean[] boolArray = new boolean[] { false, true, false, false }; 
		System.out.println(boolArray.toString());
	}
}

In the above Array toString Java Example, you can notice what will happen if you call the toString() method directly or indirectly. Here, we have tried two ways, which are directly called the toString() method and the other way of passing the array object to the toString() method.

When we pass the array object with the toString() method directly into the System.out.print() method, we get the Hashcode, but when we pass that array to the Arrays.toString() method, we get the expected output.

package myPackage;
import java.util.Arrays;
public class Test 
{
	public static void main(String[] args) 
	{
		int[][] twoD = { {100, 200, 300, 400, 500}, {300, 600, 900, 700, 800},}; 
		System.out.println("Output Using toString Method: "+Arrays.toString(twoD));
		System.out.println("Output Using deepToString Method: "+Arrays.deepToString(twoD));
	}
}

When working with multi-dimensional arrays, You should be careful about how you print them or convert them to strings—passing a multi-dimensional array into Arrays.toString() will not return an error, but the array’s contents will not be printed correctly.

To get the correct result when printing an array with more than one dimension, you must use the deepToString() method, as shown in the examples above.

Array toString in Java Example – 2

If there is some requirement, we can override the toString() default method with our own implementation. Let us take an example to understand better how to override the toString().

package myPackage;
import java.util.Arrays;
class Language 
{ 
	int Srlno; 
	String courseName;
	public Language(int rollno, String name) 
	{ 
		this.Srlno = rollno; 
		this.courseName = name; 
	} 
	@Override
	public String toString() 
	{ 
		return this.Srlno + " " + this.courseName;
	} 
} 
public class Test 
{
	public static void main(String[] args) 
	{
		Language arr[] = new Language[5];
		arr[0]=new Language(1, "Java");
		arr[1]=new Language(2, "Python");
		arr[2]=new Language(3, "Selenium");
		arr[3]=new Language(4, "Oracle");
		arr[4]=new Language(5, "Dot Net");		 
		System.out.println(Arrays.toString(arr));
	}
}

Conclusion:

After Reading this Array toString Java article, we guess you know how to convert an Array to String in Java! When you print an array using System.out.println() or any logging libraries, only the memory address and array type are printed instead of the actual content. However, you can use the Arrays.toString() or Arrays.deepToString() methods to convert Array toString Java representation that contains the actual content of the array.

If you read this article, you can smoothly use your Java programs’ “toString()” function. But if there are any questions or doubts, you can ask us in the comment section, and we will try to help you with that as soon as possible.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment