How to Convert int to String in Java
Three idiomatic ways to convert an int to a String in Java: String.valueOf(n), Integer.toString(n), and string concatenation "" + n. All produce identical results for normal integers.
String.valueOf() — recommended
int n = 42;
String s = String.valueOf(n); // "42"
Works for any primitive type (long, double, boolean, etc.) and for Object references (handles null by returning "null"). Most readable and explicit.
Integer.toString()
int n = 42;
String s = Integer.toString(n); // "42"
String binary = Integer.toString(n, 2); // "101010" (base-2)
String hex = Integer.toString(n, 16);// "2a"
Use the two-argument form when you need a different radix.
Concatenation — quick but opaque
int n = 42;
String s = "" + n; // "42"
String s2 = n + " items"; // "42 items"
Works but is less clear about intent. Fine for quick conversions or when you are building a string anyway. The compiler optimises it to StringBuilder under the hood.
String.format() — for formatted output
int price = 9;
String s = String.format("%05d", price); // "00009" (zero-padded, width 5)
String t = String.format("%,d", 1000000); // "1,000,000" (locale-aware grouping)
Formatted strings (Java 15+ text blocks / Java 21 string templates preview)
int count = 3;
String msg = "You have %d messages".formatted(count); // "You have 3 messages"
Converting Integer (boxed) to String
Integer boxed = 42;
String s = boxed.toString(); // "42"
String s2 = String.valueOf(boxed); // "42"
Both handle the unboxing automatically. String.valueOf(null) returns "null"; calling .toString() on a null Integer throws a NullPointerException.
Performance note
All three conversion methods are fast and O(1) in terms of string length (an int has at most 11 characters including sign). Do not optimise this — pick the most readable form.