The fastest way to convert an int to a String
The simplest method is String.valueOf(). Pass your integer to it, and you get a String back immediately:
int number = 42;String text = String.valueOf(number);
This is the standard approach most Java developers use because it is readable, fast, and handles edge cases like negative numbers without extra work. The result is "42" as a String, not as a number.
If you already have an Integer object (the wrapper class) instead of a primitive int, Integer.toString() works the same way and is equally fast. The choice between the two rarely matters in practice — pick whichever matches what you already have.
Key Takeaways
- String.valueOf() is the standard method and works with primitive int values, converting 42 to "42" in a single line.
- Integer.toString() does the same job when you have an Integer object instead of a primitive int.
- String concatenation with an empty string (like "" + number) works but is slower and less clear than the dedicated methods.
- String.format() lets you pad numbers with zeros or add other formatting, but costs more performance if you do not need that feature.
- All four methods handle negative numbers, zero, and extreme int values correctly without special handling.
Using Integer.toString() for Integer objects
When your variable is an Integer object (not a primitive int), call Integer.toString() directly on it:
Integer number = 42;String text = number.toString();
This is slightly more direct than String.valueOf() because you are calling the method on the object itself. Both produce identical results and run at the same speed. The choice is mostly about which form your code already has — if you have a primitive int, use String.valueOf(); if you have an Integer object, use toString().
If your Integer object might be null, String.valueOf() is safer because it will not throw a NullPointerException. It will instead return the string "null". This matters when you are working with database results or method return values that could be empty.
String concatenation: simple but slower
You can convert an int to a String by concatenating it with an empty string:
"" + number
This works because Java's concatenation operator forces the int to become a String. However, it is slower than String.valueOf() because the compiler creates a StringBuilder object behind the scenes to handle the operation. For a single conversion, the difference is tiny. In a loop that runs thousands of times, it adds up.
This method is fine for one-off conversions in simple code, but avoid it in performance-critical sections or when you are converting many numbers in sequence. It also makes your intent less clear to someone reading the code later — they have to figure out that you meant to convert, not just concatenate.
Formatting numbers with String.format()
If you need to pad your number with leading zeros or add other formatting, use String.format():
int number = 42;String text = String.format("%05d", number);
The format string "%05d" means "convert this int to a decimal number, padded with zeros to a width of 5 characters." The result is "00042". You can change the width, add commas for thousands separators, or apply other formatting rules. This is useful when you are building output for display or writing to a file where the format matters.
String.format() is more expensive than String.valueOf() because it has to parse the format string and apply the rules. Use it only when you actually need the formatting. If you just need a plain String, stick with String.valueOf().
Handling negative numbers and edge cases
All four methods handle negative numbers correctly without any extra code. String.valueOf(-42) produces "-42" as expected. The same is true for zero and for the largest and smallest possible int values (2,147,483,647 and -2,147,483,648).
The only edge case that matters is a null Integer object. If you call toString() on a null Integer, Java throws a NullPointerException. String.valueOf() handles null gracefully and returns the string "null". If null values are possible in your code, use String.valueOf() instead of toString().
Performance comparison in real code
In most applications, the performance difference between these methods is invisible. A single conversion takes microseconds either way. The difference only becomes noticeable if you are converting thousands or millions of numbers in a tight loop.
If you do need to convert many numbers at once, String.valueOf() and Integer.toString() are roughly tied for speed and both significantly faster than concatenation or String.format(). For typical application code, pick the method that reads most clearly — which is almost always String.valueOf(). Your code's readability matters more than saving a few microseconds on a conversion.
Frequently Asked Questions
What is the difference between String.valueOf() and Integer.toString()?
String.valueOf() works with primitive int values and Integer objects, and handles null safely by returning "null". Integer.toString() only works with Integer objects and throws an exception if the object is null. Both produce identical results for non-null values and run at the same speed.
Why is string concatenation slower than String.valueOf()?
When you concatenate with an empty string, Java creates a StringBuilder object behind the scenes to combine the values. String.valueOf() converts directly without that extra step. For a single conversion the difference is negligible, but in loops it adds up.
Can I convert an int to a String with leading zeros?
Yes, use String.format() with a format string like "%05d" to pad with zeros to a width of 5 characters. The result of String.format("%05d", 42) is "00042". This is slower than plain conversion, so use it only when you need the formatting.
What happens if I try to convert a null Integer object?
Calling toString() on a null Integer throws a NullPointerException. String.valueOf() handles null safely and returns the string "null". If your code might encounter null values, use String.valueOf() instead.