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 elements of the array, separated by commas and enclosed in square brackets. Any element that is 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 language but also with 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 an array of different data types, including boolean, byte, char, double, float, int long, and short. By 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 are passing the array object to the toString() method.
When we are passing the array object with the toString() method directly into the System.out.print() methods we are getting the Hashcode but when we pass that array to Arrays.toString() method we are getting 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 contents of the array will not be printed correctly.
To get the correct result when printing an array that has more than one dimension, you have to use the deepToString() method, as shown in the examples above.
Array toString in Java Example – 2
If there is some requirement then we can also override the toString() default method with our own implementation. Let us take an example to better understand 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 now 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 type of array 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 will be able to use the “toString()” function in your Java programs smoothly. But if there is any question or doubts then you can ask us in the comment section and we will try to help you with that as soon as possible.
Leave a Reply