How do I convert from int to String in Java?
To convert an int
to a String
in Java, you can use the Integer.toString()
method. For example:
int x = 42;
String s = Integer.toString(x);
Alternatively, you can use the String.valueOf()
method:
int x = 42;
String s = String.valueOf(x);
You can also use the +
operator to concatenate an int
with an empty string, which will cause the int
to be converted to a String
:
int x = 42;
String s = "" + x;
Finally, you can use string interpolation, which is available in Java since version 15:
int x = 42;
String s = String.format("%d", x);