Java toString to Convert Array to String

The Java toString Method is one of the Array Methods to return the string representation of the user-specified array. This article will show how to convert the Array to String using Arrays.toString with an example.

Java toString Syntax

The Java Programming Language provides nine array toString methods to convert the Array to String.

// Boolean array to string
public static String toString(Boolean[] anBooleanArray); // It will return String
//In order to use in program
Arrays.toString(Boolean[] anBooleanArray);

// Byte
public static String toString(byte[] anByteArray);
Arrays.toString(byte[] anByteArray);

// Short
public static String toString(short[] anShortArray);
Arrays.toString(short[] anShortArray);

// Char
public static String toString(char[] anCharArray); 
Arrays.toString(char[] anCharArray);

// Int 
public static String toString(int[] anIntArray); 
Arrays.toString(int[] anIntArray);

// Long 
public static String toString(long[] anLongArray); 
Arrays.toString(long[] anLongArray);

// Double 
public static String toString(double[] anDoubleArray);
Arrays.toString(double[] anDoubleArray);

// Float 
public static String toString(float[] anFloatArray);
Arrays.toString(float[] anFloatArray);

//  Converts the Array of Objects to string.
public static String toString(Object[] anObjectArray); 
Arrays.toString(Object[] anObjectArray);

The Java Array toString Method accepts different types of arrays as the argument to convert them to String. It returns the String representation of the content. The string representation means a list of array elements enclosed in “[ ]”.

NOTE: If you specify the Null value as the argument, the Array toString method will return NULL as output.

Converting Java Byte Array to String using toString

In this program, we declared the byte array with random elements. Then we will call the public static String toString (byte[] anByteArray) method to convert the Byte array to string.

package ArrayMethods;
import java.util.Arrays;

public class ByteToString {
	public static void main(String[] args) {
		byte[] byteArray = {12, 3, 2, 18, 45}; 
		
		System.out.println("Original Byte Array is :");
		arrayPrint(byteArray);
		
		System.out.println("\nThe String Representation of an Byte Array is:");
		System.out.println(Arrays.toString(byteArray));
	}
	public static void arrayPrint(byte[] bitArray) {
		for (byte Number: bitArray) {
			System.out.println("Item = " + Number);
		}
	}

}
Original Byte Array is :
Item = 12
Item = 3
Item = 2
Item = 18
Item = 45

The String Representation of an Byte Array is:
[12, 3, 2, 18, 45]

The following statement is to print the Byte array elements to the output

arrayPrint(byteArray);

When the compiler reaches the above statement, the compiler will jump to the following function. From the below code snippet, observe that we used Java For Loop to iterate the Byte Array. Then we print every element using the System.out.println statement.

public static void arrayPrint(byte[] bitArray) {
 for (byte Number: bitArray) {
 System.out.println("Array Elelment = " + Number);
 }
 }

Lastly, we placed this method directly inside our System.out.println statement. It will convert the Java byte array to string and returns the output.

System.out.println(Arrays.toString(byteArray));

Converting Boolean Array to String

In this Java toString program, we declare the Boolean array with random elements. Then we will call the public static String toString (boolean[] anBooleanArray) method to convert the Boolean array to string.

package ArrayMethods;
import java.util.Arrays;

public class BooleanToString {
	public static void main(String[] args) {
		boolean[] bool = {true, false, false, true};
		
		System.out.println("The String Representation of an Boolean Array is:");
		System.out.println(Arrays.toString(bool));
	}
}
Java Array to String 1

Converting Java Short Array to String using toString

In this program, we declared the short array with random elements. Then we will call the public static String toString (short[] anShortArray) method to convert the short array to a string.

package ArrayMethods;
import java.util.Arrays;

public class ShortToString {
 public static void main(String[] args) {
 short[] ShortArray = {1, 5, 8, 7, 3}; 
 
 System.out.println("The String Representation of an Short Array is:");
 System.out.println(Arrays.toString(ShortArray));
 }
}
The String Representation of an Short Array is:
[1, 5, 8, 7, 3]

Converting Java Int Array to String using toString

In this program, we declare the integer array with random elements. Then we will call the public static String toString (int[] anIntegerArray) method to convert the Int array to a string.

package ArrayMethods;
import java.util.Arrays;

public class IntToString {
	public static void main(String[] args) {
		int[] IntegerArray = {40, 25, 15, 95, 36}; 
		
		System.out.println("The String Representation of an Integer Array is:");
		System.out.println(Arrays.toString(IntegerArray));
	}
}
The String Representation of an Integer Array is:
[40, 25, 15, 95, 36]

Converting Long Array to String

In this program, we declared the long array with random elements. Then we will call the Java public static String toString (long[] anLongArray) method to convert the Long array to a string.

package ArrayMethods;
import java.util.Arrays;

public class LongToString {
	public static void main(String[] args) {
		long[] LongArray = {180, 150, 280, 990, 1060};  
		
		System.out.println("The String Representation of an Long Array is:");
		System.out.println(Arrays.toString(LongArray));
	}
}
The String Representation of an Long Array is:
[180, 150, 280, 990, 1060]

Converting Double Array to String

In this program, we declared the double array with random elements. Next, call the Java public static String toString (double[] anDoubleArray) method to convert the double array to a string.

package ArrayMethods;
import java.util.Arrays;

public class DoubleToString {
	public static void main(String[] args) {
		double[] DoubleArray = {140.25, 125.965, 18.835, 39.363, 65.894}; 
		
		System.out.println("The String Representation of an Double Array is:");
		System.out.println(Arrays.toString(DoubleArray));
	}
}
The String Representation of an Double Array is:
[140.25, 125.965, 18.835, 39.363, 65.894]

Converting Java Float Array to String using toString

We declared the floating point array with random elements in this Java program. Then we will call the public static String toString (float[] anFloatArray) method to convert the float array to a string.

package ArrayMethods;
import java.util.Arrays;

public class FloatToString {
	public static void main(String[] args) {
		float[] fltArray = {4.25f, 2.95f, 8.83f, 3.36f, 5.94f}; 
		
		System.out.println("The String Representation of an Float Array is:");
		System.out.println(Arrays.toString(fltArray));
	}
}
The String Representation of an Float Array is:
[4.25, 2.95, 8.83, 3.36, 5.94]

Convert Char Array to String

In this Java program, we declare the character array with random elements. Then, we will call the public static String toString (char[] anCharacterArray) method to convert the Char array to a string.

package ArrayMethods;
import java.util.Arrays;

public class CharToString {
	public static void main(String[] args) {
		char[] CharArray = {'t', 'u', 't', 'o', 'r', 'i', 'a','l', 's'}; 
		
		System.out.println(Arrays.toString(CharArray));
	}
}
[t, u, t, o, r, i, a, l, s]

Convert Object Array to String

In this toString program, we declare the Object array with random elements. Next, we call the public static String toString (Object[] anObjectArray) method to convert the Object array to a string.

package ArrayMethods;
import java.util.Arrays;

public class ObjectToString {
	public static void main(String[] args) {
		Object[] objArray = new Object[5]; 
		objArray[0] = 200;
		objArray[1] = "Java Programming";
		objArray[2] = 12.65;
		objArray[3] = 9.25f;
		objArray[4] = new StringBuilder("Tutorial");
		
		System.out.println("The String Representation of an Object Array is:");
		System.out.println(Arrays.toString(objArray));
	}
}
The String Representation of an Object Array is:
[200, Java Programming, 12.65, 9.25, Tutorial]