Convert int to String in Java
Problem
Converting a number to a String produces its text form — the characters you would see when printing it — so it can be joined, measured or stored as text.
Given an int, convert it to a String in three different ways and confirm the results are equal.
Java Program
public class IntToString {
public static void main(String[] args) {
int score = 907;
String a = String.valueOf(score); // works for every primitive type
String b = Integer.toString(score); // the method valueOf calls for an int
String c = "" + score; // concatenation also converts, but builds an extra string
System.out.println(a + " | " + b + " | " + c);
System.out.println("Length of the text: " + a.length());
System.out.println("All equal: " + (a.equals(b) && b.equals(c)));
}
}Output
Core Logic
All three techniques produce the same text; they differ only in readability and in how much extra work happens behind the scenes.
- 1
String.valueOf(score)has overloads for every primitive type, which makes it a safe default. - 2
Integer.toString(score)is the method thatvalueOfcalls for an int. - 3
"" + scoreconverts through string concatenation, which builds an extra temporary string. - 4
a.length()works only becauseais now text: 907 has three characters.
"907", so equals reports true.Key Point: Compare the converted strings with equals, never == — == checks whether two references point to the same object, not whether the text matches.
Key Concepts
Approach 2: Manual Conversion (Digit by Digit)
public class IntToStringManual {
public static void main(String[] args) {
System.out.println(toText(907));
System.out.println(toText(-4120));
System.out.println(toText(0));
}
static String toText(int number) {
if (number == 0) return "0";
boolean negative = number < 0;
long n = Math.abs((long) number); // long, so Integer.MIN_VALUE doesn't overflow
StringBuilder digits = new StringBuilder();
while (n > 0) {
digits.append((char) ('0' + n % 10)); // last digit first
n /= 10;
}
if (negative) digits.append('-');
return digits.reverse().toString();
}
}
Output
Core Logic
Repeatedly taking n % 10 extracts digits from right to left, and '0' + digit turns each one into its character.
- 1Zero is handled up front, because the loop would produce an empty string for it.
- 2The sign is recorded, and the absolute value is taken as a
longsoInteger.MIN_VALUEdoesn't overflow. - 3
(char) ('0' + n % 10)appends the last digit as a character, thenn /= 10drops it. - 4A minus sign is added if needed, and the builder is reversed.
-4120, the digits come out as 0, 2, 1, 4, the minus sign is appended, and reversing gives "-4120".Key Point: The long cast is the subtle part: Math.abs(Integer.MIN_VALUE) is still negative in int arithmetic, which would break the loop.
Why: The loop runs once per digit d, and the builder holds one character per digit.
Key Concepts
Interview Questions
What's the fastest way to convert an int to a String?
String.valueOf(n) and Integer.toString(n) are equivalent and efficient. "" + n works but creates an extra temporary string.
How do you pad a number with leading zeros?
String.format("%05d", 42) gives "00042".