This post will discuss how to convert an int array to a String in Java.

1. Using Stream API

You can Java 8 Streams to easily convert an int array to a string. The idea is to get an IntStream for the int array and map each element of the stream to a String object. Then perform a reduction operation on stream elements using the Stream.reduce() method that returns an Optional describing the reduced object. This is demonstrated below:

Download  Run Code

Output:

1, 2, 3, 4, 5

 
Here’s a version that works with an Integer array:

Download  Run Code

Output:

1, 2, 3, 4, 5

2. Using Arrays.toString() method

If you just need to print the string representation of an integer array, you can do so using the Arrays.toString() method. It returns the array’s elements, enclosed in square brackets [] where the adjacent elements are separated by ", ".

Download  Run Code

Output:

[1, 2, 3, 4, 5]

 
If you need to remove [ and ] from the output, you can use the replaceAll() method on the string representation.

Download  Run Code

Output:

1, 2, 3, 4, 5

3. Using Custom Routine

Finally, you can write your custom method to convert an int array to a String. The following solution creates a new string and concatenates each array element to it, separated by ", ".

Download  Run Code

Output:

1, 2, 3, 4, 5

That’s all about converting an int array to a String in Java.