Java Array to String

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 using Arrays.toString with an example.

Syntax of toString to convert Java Array to String

The Java Programming Language provides nine different array toString methods to convert the Java 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 array to string
public static String toString(byte[] anByteArray);
Arrays.toString(byte[] anByteArray);

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

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

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

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

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

// Float array to string
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 the Java 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 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 the Java For Loop to iterate the Byte Array. Then we are printing 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 Java Boolean Array to String

In this Java array to string program, we declared the Boolean array with random elements. Then we will call the public static String toString (boolean[] anBooleanArray) method to convert the 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 elements. Then we will call the public static String toString (short[] anShortArray) method to convert the 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 elements. Then we will call the public static String toString (int[] anIntegerArray) method to convert the 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 array to string program, we declared the long array with random elements. Then we will call the public static String toString (long[] anLongArray) method to convert the 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 elements. Next, call the public static String toString (double[] anDoubleArray) method to convert the 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 elements. Then we will call the public static String toString (float[] anFloatArray) method to convert the 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 elements. Then, we will call the public static String toString (char[] anCharacterArray) method to convert the 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 Object with random elements. Next, we call the public static String toString (Object[] anObjectArray) method to convert the 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]