Concatenate integers to a String in Java
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { int a = 1; int b = 2; int c = 3; String s = "" + a + b + c; System.out.println(s); // 123 } } |
Instead of using an empty string literal, you can use the String.valueOf() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { int a = 1; int b = 2; int c = 3; String s = String.valueOf(a) + b + c; System.out.println(s); // 123 } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { int a = 1; int b = 2; int c = 3; String s = String.format("%s%s%s", a, b, c); System.out.println(s); // 123 } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.stream.Collectors; public class Main { public static String concatenate(int... digits) { return Arrays.stream(digits) .mapToObj(String::valueOf) .collect(Collectors.joining()); } public static void main(String[] args) { int a = 1; int b = 2; int c = 3; String s = concatenate(a, b, c); System.out.println(s); // 123 } } |
4. Using StringBuilder
Here’s an equivalent version using StringBuilder. This works with older versions of Java.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class Main { public static String concatenate(int... digits) { StringBuilder sb = new StringBuilder(); for (int digit : digits) { sb.append(digit); } return sb.toString(); } public static void main(String[] args) { int a = 1; int b = 2; int c = 3; String s = concatenate(a, b, c); System.out.println(s); // 123 } } |
You can avoid a for loop if you have less number of items to append:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { int a = 1; int b = 2; int c = 3; String s = new StringBuilder().append(a).append(b).append(c).toString(); System.out.println(s); // 123 } } |
That’s all about concatenating integers to a String in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)