How do I convert from int to String?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Java, converting an int to a String is simple, but the best method depends on the situation. Most of the time, String.valueOf or Integer.toString is the right answer. The interesting part is not whether conversion is possible, but which API communicates intent clearly and avoids surprises when the value is not a primitive.
The usual answer: String.valueOf
For general application code, String.valueOf is the safest default. It is short, explicit, and works naturally when you are converting a primitive int.
This is the form many teams standardize on because it reads well and works consistently in ordinary code.
Integer.toString is equally valid
If you want to emphasize that the conversion is specifically about an integer, Integer.toString is also a good option.
For primitive int values, there is no practical difference in normal business code. This is mostly a style choice.
String concatenation works, but it is not the clearest tool
Java will convert an integer to text automatically if you concatenate it with a string.
This works, but it is often less clear than calling a conversion method directly. If the whole goal is conversion, String.valueOf(count) communicates intent better than relying on concatenation side effects.
Formatting is the right choice when presentation matters
Sometimes you are not merely converting a value to text. You are formatting it for display, logging, or export. In those cases, use formatting APIs instead of a plain conversion.
Here the second string is not just "27". It is "00027", which is a presentation rule, not a basic type conversion.
Wrapper types add one important nuance
The most useful edge case to understand is the difference between a primitive int and an Integer object reference. If you have an Integer that might be null, toString() can throw a NullPointerException, while String.valueOf handles null by returning the string "null".
This distinction matters most in code that receives boxed values from collections, frameworks, or optional database fields.
Common Pitfalls
The most common mistake is using string concatenation when the code is really doing a plain conversion. It works, but it hides the intent.
Another issue is forgetting the difference between int and Integer. A primitive cannot be null, but a boxed Integer can, and calling toString() on it will fail.
Developers also use simple conversion when they really need formatting. If the output must include padding, grouping, or locale-aware rendering, use formatting APIs instead of String.valueOf.
Finally, avoid overthinking micro-differences between String.valueOf and Integer.toString in normal code. Readability and consistency matter more.
Summary
- In Java,
String.valueOf(int)is the safest general-purpose way to convert anintto aString. - '
Integer.toString(int)is equally valid and often used for clarity around integer conversion.' - String concatenation works, but it is usually less explicit than a real conversion call.
- Use
String.formatwhen you need formatted output rather than plain conversion. - Be careful with
Integerwrappers, becausetoString()onnullthrows an exception.
Related reading
- How do I convert Long to byte and back in java
- How do I copy an object in Java?
- How do I count the number of occurrences of a char in a String?
- How do I create a unique ID in Java?
- How do I create an immutable Class?
- How do I create beans programmatically in Spring Boot?
- How do I decompile Java class files?
- How do I define a method which takes a lambda as a parameter in Java 8?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.