How to Convert Array to String in Java with Example

Array and String are very closely related, not with anything else but with popularity. Many times we need to convert an array to String or create an array from String, but unfortunately, there is no direct way of doing this in Java. Though you can convert an array to String by simply calling their toString() method, you will not get any meaningful value.  If you convert an integer array to String, you will get something like I@4fee225 due to the default implementation of toString() method from java.lang.Object class. First, I show the type of the array and content after @ is hash code value in hexadecimal. How valuable is that? This is not what I want to see, I was interested in contents rather than hashcode. Fortunately, Java provides a utility class called java.util.Arrays, which provides several utility methods for arrays in Java.

For example, here we have method to sort array, search elements using binary search, fill array, methods to check if two arrays are equal or not, copy range of values from one array to another,  and much needed toString() and deepToString() method to convert both one-dimensional and multi-dimensional array to String.

This method provides the content view of the array, for example, when you convert an integer array {1, 2, 3, 4, 5, 6, 7} to String, you will get [1, 2, 3, 4, 5, 6, 7] instead of  [I@2ab6994f .

In this article, we will see examples to convert different types of the array to String e.g. int, char, byte, double, float, Object and String array itself.  We will also learn how to convert a two-dimensional array to String in Java.





Array to String in Java

Here is our sample Java program to convert an array to String in Java. If you want to run this program in Eclipse all you have to do is, create a Java project in Eclipse, copy this code, right click on the src folder on your Java project in Eclipse, and rest will be taken care by IDE. It will take care of creating a proper package and Java source file. You don't have to manually create the package and then Java file by your own. In these examples, I have first shown what will happen if you call toString() method directly or indirectly (by passing array to System.out.print() methods) and then right way to convert it to String by passing array to Arrays.toString() method. Care should be taken while printing multi-dimensional array or converting them to String. It's not an error when you pass a multi-dimensional array to Arrays.toString() but it will not print it correctly. You must use deepToString() method to convert array which has more than one dimension, as shown in the last couple of examples of printing two and three-dimensional arrays in Java.

import java.util.Arrays;

/**
 * Java Program to convert array to String in Java. In this tutorial you will
 * learn how to convert integer array, char array, double array, byte array,
 * multi-dimensional array to String in Java.
 *
 * @author Javin Paul
 */
public class ArrayToString {

    public static void main(String args[]) {

        // Converting int array to String in Java
        int[] numbers = {1, 2, 3, 4, 5, 6, 7};
        System.out.println(numbers.toString());

        String str = Arrays.toString(numbers);
        System.out.println("int array as String in Java : " + str);

        // Converting char array to String in Java
        char[] vowels = {'a', 'e', 'i', 'o', 'u'};
        System.out.println(vowels.toString());

        String charArrayAsString = Arrays.toString(vowels);
        System.out.println("char array as String in Java : " + str);

        // Converting byte array to String in Java
        byte[] bytes = {(byte) 0x12, (byte) 0x14, (byte) 0x16, (byte) 0x20};
        System.out.println(bytes.toString());

        String byteArrayAsString = Arrays.toString(bytes);
        System.out.println("byte array as String in Java : " + byteArrayAsString);

        // Converting float array to String in Java
        float[] floats = {0.01f, 0.02f, 0.03f, 0.04f};
        System.out.println(floats.toString());

        String floatString = Arrays.toString(floats);
        System.out.println("float array as String in Java : " + floatString);

        // Converting double array to String in Java
        double[] values = {0.5, 1.0, 1.5, 2.0, 2.5};
        System.out.println(values.toString());

        String doubleString = Arrays.toString(values);
        System.out.println("double array as String in Java : " + doubleString);

        // Converting object array to String in Java
        Object[] objects = {"abc", "cdf", "deg", "england", "india"};
        System.out.println(objects.toString());

        String objectAsString = Arrays.toString(objects);
        System.out.println("object array as String in Java : " + objectAsString);

        // Convert two dimensional array to String in Java
        int[][] twoD = {
            {100, 200, 300, 400, 500},
            {300, 600, 900, 700, 800},};

        System.out.println(twoD.toString());

        String twoDimensions = Arrays.deepToString(twoD);
        System.out.println("Two dimensional array as String in Java : " + twoDimensions);

        // Convert three dimensional array to String in Java
        int[][][] threeD = {
            {
                {11, 22, 33, 44, 55},
                {32, 42, 52, 62, 72},},
            {
                {1111, 2222, 3333, 4444, 5555},
                {1001, 2001, 3001, 4001, 5001},}
        };
        System.out.println(threeD.toString());

        String threeDString = Arrays.deepToString(threeD);
        System.out.println("3 dimensional array as String in Java : " + threeDString);

    }

}

Output
[I@2ab6994f
int array as String in Java : [1, 2, 3, 4, 5, 6, 7]
[C@3a0b2771
char array as String in Java : [1, 2, 3, 4, 5, 6, 7]
[B@324a897c
byte array as String in Java : [18, 20, 22, 32]
[F@3b8845af
float array as String in Java : [0.01, 0.02, 0.03, 0.04]
[D@5bfd9b49
double array as String in Java : [0.5, 1.0, 1.5, 2.0, 2.5]
[Ljava.lang.Object;@66de04cd
object array as String in Java : [abc, cdf, deg, england, india]
[[I@4fee225
Two dimensional array as String in Java : [[100, 200, 300, 400, 500], [300, 600, 900, 700, 800]]
[[[I@cde6570
3 dimensional array as String in Java : [[[11, 22, 33, 44, 55], [32, 42, 52, 62, 72]], [[1111, 2222, 3333, 4444, 5555], [1001, 2001, 3001, 4001, 5001]]]


That's all about how to convert an array to String in Java. This is also a good lesson on how you can print any array in the readable format. Even Java debuggers like what is available in Eclipse, Netbeans and IntelliJ IDEA print array like that. Currently, if you try to watch an array in Eclipse, you will see it's proper content,  instead of default type@hashcode values, as seen in the ollowing screenshot.
Array to String in Java with Example

2 comments:

  1. I remember, in one of our project we need to convert array of String to one String where each element is separated with comma. It was simple to do, just iterate throught Array and use an Stringbuffer to append each element at the end after comma.

    ReplyDelete
  2. String charArrayString = Arrays.toString(vowels);
    System.out.println("char array as String in java :" + charArrayString);

    ReplyDelete