The simplest way to convert an integer to a string in Java
The fastest method is Integer.toString(). Pass your integer to it, and you get a string back in one line:
int number = 42; String text = Integer.toString(number); // text is now "42"
This works for any integer value, positive or negative. If you have an Integer object (the wrapper class, not the primitive type), call .toString() directly on it instead: Integer.toString(myInteger) or myInteger.toString().
Integer.toString() is the clearest choice because the name tells you exactly what it does, and it runs fast because it does one job only.
Key Takeaways
- Integer.toString() is the standard method — pass an int and get a String back in a single, readable line.
- String.valueOf() does the same thing and handles null values without crashing, making it safer in some situations.
- String concatenation with an empty string ("" + number) works but is slower and less clear about your intent.
- String.format() lets you control how the number looks — padding, leading zeros, or hexadecimal — when you need formatting beyond a plain conversion.
Using String.valueOf() when you want built-in null safety
The String.valueOf() method does the same conversion as Integer.toString(), but it handles null values differently. If you pass null to Integer.toString(), the program crashes. If you pass null to String.valueOf(), it returns the string "null" instead.
Integer number = null; String text = String.valueOf(number); // text is now "null", no crash
Use String.valueOf() when your integer might be null and you want the program to keep running. Use Integer.toString() when you know the value is not null and you want to be explicit about that assumption.
String concatenation: readable but slower
You can convert an integer to a string by adding it to an empty string. Java automatically converts the integer to a string during the concatenation:
int number = 42; String text = "" + number; // text is now "42"
This method works and is straightforward to read, but it creates an extra String object in memory even though you only need one. For a single conversion, the difference is tiny. For a loop that runs thousands of times, it adds up. Stick with Integer.toString() or String.valueOf() unless you are already building a larger string.
Using String.format() when you need to control how the number looks
The String.format() method lets you specify exactly how the number should appear. You write a format string that describes the output, then pass the integer:
int number = 42; String padded = String.format("%05d", number); // padded is now "00042"
The format string %05d means "convert to a decimal integer and pad with zeros to 5 characters total." Other common formats include %x for hexadecimal (base 16) and %o for octal (base 8). Use String.format() only when you need this kind of control, because it is slower than the other methods and the format string syntax takes practice to read.
When each method makes sense in real code
In most situations, use Integer.toString(). It is fast, clear, and handles all normal cases. Choose String.valueOf() if your integer might be null and you want the program to continue running. Use concatenation only if you are already building a larger string and adding the number is just one part of it. Use String.format() only when you need to control padding, decimal places, or number base.
If you are converting many integers in a loop, Integer.toString() is the right choice. If you are converting a single integer once, any of these methods is fine — pick the one that reads most clearly to someone else reading your code later.
Converting Integer objects versus int primitives
Java has two types: the primitive int and the wrapper class Integer. Both convert to strings the same way, but the syntax is slightly different. For a primitive int, use Integer.toString(). For an Integer object, you can call .toString() directly on it:
int primitive = 42; String text1 = Integer.toString(primitive); Integer object = 42; String text2 = object.toString();
Both produce "42". The difference matters only if you are reading code and need to know whether you are working with a primitive or an object. In practice, Java converts between them automatically in most situations, so you rarely have to think about it.
Frequently Asked Questions
What happens if I convert a negative integer?
All four methods handle negative integers correctly. Integer.toString(-42) returns "-42". The minus sign is included in the string, and all formatting options work the same way with negative numbers as they do with positive ones.
Can I convert a very large integer?
If your number is larger than what an int can hold (about 2 billion), use a long instead and call Long.toString(). The syntax is identical: Long.toString(largeNumber). Java has separate types for different size ranges, and each has its own toString() method.
Why does String.format() seem slower?
String.format() parses the format string every time you call it, which takes extra work. Integer.toString() just converts the number directly. For a single conversion, you will not notice the difference. In a loop that runs a million times, String.format() will be noticeably slower. Use it only when you need the formatting features.
What if I need to convert a string back to an integer?
Use Integer.parseInt(). Pass it a string like "42" and it returns the integer 42. If the string does not contain a valid number, the program crashes, so wrap it in a try-catch block if the string might be invalid.