Convert int to String in Java
Converting an int to a String is trivial in Java β but there are several ways to do it, and knowing which one to pick makes code shorter, clearer, and faster in hot paths.
The three main approaches
int n = 42;
String a = String.valueOf(n); // "42" β recommended
String b = Integer.toString(n); // "42"
String c = "" + n; // "42" β concise but not ideal
String d = String.format("%d", n); // "42" β when you need formatting
All four produce the same result for simple cases. The difference is in readability, performance, and what they do with null.
String.valueOf β the idiomatic choice
String s = String.valueOf(42); // "42"
String t = String.valueOf(3.14); // "3.14"
String u = String.valueOf(true); // "true"
String v = String.valueOf((Integer)null); // "null" β safe
Works on any primitive or object. For Integer (boxed), returns "null" instead of throwing NullPointerException.
Integer.toString β equivalent for primitives, with radix option
Integer.toString(42); // "42"
Integer.toString(42, 2); // "101010" β binary
Integer.toString(255, 16); // "ff" β hex (lowercase)
Integer.toString(8, 8); // "10" β octal
The two-argument form converts to any base from 2 to 36. Use it for bit masks, permission codes, or anytime you're working in non-decimal.
Concatenation β convenient but watch out
int n = 42;
String s = "" + n; // "42"
System.out.println("n=" + n); // usually fine
Inside a single expression, this is fine β the compiler uses StringBuilder behind the scenes. But in a loop, repeated concatenation allocates a new string every iteration. Use StringBuilder:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++) {
sb.append(i).append(',');
}
String result = sb.toString();
String.format β when you need padding or grouping
String.format("%d", 42); // "42"
String.format("%05d", 42); // "00042" β left-padded with zeros
String.format("%5d", 42); // " 42" β right-aligned, width 5
String.format("%-5d|", 42); // "42 |" β left-aligned
String.format("%,d", 1_000_000); // "1,000,000" β thousand separators
Java 15+: String.formatted
String s = "%,d items".formatted(12345); // "12,345 items"
From int to hex, binary, octal
Integer.toHexString(255); // "ff"
Integer.toBinaryString(10); // "1010"
Integer.toOctalString(8); // "10"
Formatting with locale
import java.text.NumberFormat;
import java.util.Locale;
NumberFormat us = NumberFormat.getInstance(Locale.US);
NumberFormat fr = NumberFormat.getInstance(Locale.FRANCE);
us.format(1234567); // "1,234,567"
fr.format(1234567); // "1 234 567" (narrow no-break space)
Which one should you use?
| Goal | Use |
|---|---|
| Quick, readable conversion | String.valueOf(n) |
| Hex / binary / other base | Integer.toString(n, base) |
| Formatted output (padding, grouping) | String.format(...) or n + "items" |
| Inside a log message | concatenation |
| Inside a tight loop | StringBuilder |
| Null safety with boxed Integer | String.valueOf |
Performance note
For a single conversion, all four approaches are fast. Differences only matter in tight loops processing millions of values. Benchmarks typically show:
Integer.toString(n) β fastest (direct JDK path)
String.valueOf(n) β same (delegates to Integer.toString)
""+ n β slightly slower (StringBuilder allocation)
String.format("%d", n) β slowest (parser + format engine)
Optimize only when profiling says to.
Common mistakes
- Writing
(String) nβ compile error, you can't cast primitives to String. - Relying on
Integer.toString(null)β Integer wrapper, NullPointerException at call time. UseString.valueOf. - Calling
String.formatin tight loops for simple conversions. - Expecting locale separators from
String.valueOfortoStringβ neither adds them. UseNumberFormat.
For 95% of code, String.valueOf(n) is the right default. Reach for other methods only when you need a specific format or base.