This post will discuss how to concatenate integers to a String in Java.

1. Using String concat (+)

You can use the String concatenation operator + to concatenate integers to a string in a clear and concise manner. Note that the compiler implicitly constructs an intermediate StringBuilder object, append the integers, and then call the toString() method.

Download  Run Code

 
Instead of using an empty string literal, you can use the String.valueOf() method:

Download  Run Code

2. Using String.format() method

Another plausible way is to use the String.format() method to get the formatted string from the specified integer arguments using the %s format specifier.

Download  Run Code

3. Using Stream API

If you have several integers that needed to be concatenated to a string, it’s better to create a utility method that accepts a vararg. If you use Java 8 or above, you can use the joining collector:

Download  Run Code

4. Using StringBuilder

Here’s an equivalent version using StringBuilder. This works with older versions of Java.

Download  Run Code

 
You can avoid a for loop if you have less number of items to append:

Download  Run Code

That’s all about concatenating integers to a String in Java.