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
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. For primitive int values, it behaves identically to Integer.toString():
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.format() for formatted conversion:
int x = 42;
String s = String.format("%d", x);