Java Array to String

The Java toString Method is one of the Array Methods, which is to return the string representation of the user-specified array. In this article, we will show how to convert the Java Array to String with an example. The basic syntax of the Arrays.toString in Java Programming language is as shown below.

Syntax of toString to convert Java Array to String

The Java Programming Language provides nine different methods to convert the Java Array to String. The following Java Arrays.toString method will accept the Boolean Array as the parameter and converts the Java Boolean array to string.

public static String toString(Boolean[] anBooleanArray); // It will return String

//In order to use in program
Arrays.toString(Boolean[] anBooleanArray);

It will accept the Byte Array as the parameter and converts the Java Byte array to string.

public static String toString(byte[] anByteArray); // It will return String

//In order to use in program
Arrays.toString(byte[] anByteArray);

The following Java array.tostring method will accept the short Array as the parameter and converts the Java Short array to string.

public static String toString(short[] anShortArray); // It will return String

//In order to use in program
Arrays.toString(short[] anShortArray);

This Java Arrays.toString method will accept the Character Array as the parameter and converts the Java Char array to string.

public static String toString(char[] anCharArray); // It will return String

//In order to use in program
Arrays.toString(char[] anCharArray);

It will accept the Integer Array as the parameter and converts the Java Int array to string.

public static String toString(int[] anIntArray); // It will return String

//In order to use in program
Arrays.toString(int[] anIntArray);

The following method will accept the Long Array as the parameter and converts the Java Long array to string.

public static String toString(long[] anLongArray); // It will return String

//In order to use in program
Arrays.toString(long[] anLongArray);

This Java Arrays.toString method will accept the Double Array as the parameter and converts the Java Double array to string.

public static String toString(double[] anDoubleArray); // It will return String

//In order to use in program
Arrays.toString(double[] anDoubleArray);

It will accept the Float Array as the parameter and converts the Java Float array to string.

public static String toString(float[] anFloatArray); // It will return String

//In order to use in program
Arrays.toString(float[] anFloatArray);

This Arrays.toString method will accept the Object Array (Array of Objects) as the parameter and converts the Array of Objects to string.

public static String toString(Object[] anObjectArray); // It will return String

//In order to use in program
Arrays.toString(Object[] anObjectArray);

The Java Arry.toString Method accepts different types of arrays as the argument to convert the array 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

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

// Example to Convert Java 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("Array Elelment = " + Number);
		}
	}

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

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

Within this Java Arrays.toString example, we declared a byte Array and assigned some random values as the array elements.

byte[] byteArray = {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 the Java For Loop to iterate the Byte Array. Then we are printing every array 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 the Arrays.toString 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 Java Boolean Array to String

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

// Example to Convert Java 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

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

// Example to Convert Java Short Array to 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

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

// Example to Convert Java Int Array to 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 Java Long Array to String

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

// Example to Convert Java Long Array to 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 Java Double Array to String

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

// Example to Convert Java Double Array to 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

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

// Example to Convert Java Float Array to 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]

How to Convert Java Char Array to String

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

// Example to Convert Java Char Array to 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("The String Representation of an Character Array is:");
		System.out.println(Arrays.toString(CharArray));
	}
}
The String Representation of an Character Array is:
[t, u, t, o, r, i, a, l, s]

Convert Java Object Array to String

In this Java array tostring program, we declared the String array (string is an Object) with random array elements. Next, we call the public static String toString (Object[] anObjectArray) method to convert the Object array to string.

// Example to Convert Java Object Array to 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]