How to Convert an int to a String in Java
Convert a Java int to a string with Integer.toString, String.valueOf, and concatenation.
How to Convert an int to a String in Java
Turning an int into a String is one of the most common conversions in Java — you need it whenever you log a number, build a message, or write a value to a file. The JDK offers several ways to do it, and they all produce the same text. This chapter shows the idiomatic options and when to reach for each.
Integer.toString — the direct call
The clearest, most explicit way is Integer.toString(int). It says exactly what it does and returns the decimal text of the number:
int n = 256;
String s = Integer.toString(n); // "256"This is the method String.valueOf and concatenation both end up calling internally, so it is the canonical conversion. Negative values keep their sign (Integer.toString(-7) returns "-7"), and the result is never null.
String.valueOf — null-safe and overloaded
String.valueOf is overloaded for every primitive type, so the same call works for int, long, double, boolean, and char:
int n = 256;
String s = String.valueOf(n); // "256"For an int it simply delegates to Integer.toString, so the output is identical. Many developers prefer it because the single name covers all primitives and it is the conventional choice when converting an object reference that might be null (it returns "null" instead of throwing).
String concatenation — quick but watch the cost
Appending an int to an empty string forces a conversion:
int n = 256;
String s = "" + n; // "256"It is concise and fine for a one-off, but the compiler turns it into a StringBuilder operation. Inside a loop, repeated "" + n builds many throwaway objects; prefer Integer.toString or a single StringBuilder there.
String.format and radix conversions
When you need padding, a sign, or a non-decimal base, use String.format or the radix-aware Integer methods:
String padded = String.format("%05d", 42); // "00042"
String hex = Integer.toString(255, 16); // "ff"
String binary = Integer.toBinaryString(255); // "11111111"Here is how the everyday options compare:
| Approach | Best for | Note |
|---|---|---|
Integer.toString(n) | Plain decimal text | Canonical; never null |
String.valueOf(n) | One name for all primitives | Delegates to Integer.toString |
"" + n | Quick inline use | Avoid in tight loops |
String.format("%05d", n) | Padding / formatting | Locale-aware, slower |
Integer.toString(n, radix) | Hex, binary, octal text | Radix 2–36 |
A worked example
This program runs every approach on the same value, confirms the three common methods produce equal strings, and shows formatting and radix output.
What to take from the run:
Integer.toString(-42),String.valueOf(-42), and"" + (-42)all print-42— the three common ways are interchangeable for plain decimal text.- The
equal?line printstrue, proving the three results are character-for-character identical, becauseString.valueOfand concatenation both route throughInteger.toString. - The negative sign survives the conversion, and the resulting string has length
3(-,4,2) — the minus counts as a character. String.format("%05d", 42)prints00042, showing how the%05ddirective zero-pads to a minimum width of five.Integer.toBinaryString(255)prints11111111andInteger.toString(255, 16)printsff, demonstrating that anintcan be rendered in any base, not just decimal.
Practice
Which call converts an int to its plain decimal String form and is the canonical method that String.valueOf(int) delegates to internally?