The simplest ways to turn a number into text
When you have a number in Java and need to work with it as text — to display it, save it to a file, or combine it with other text — you convert it to a string. Java gives you three main routes, and which one you use depends on what you're doing with the result.
The fastest method is String.valueOf(). If you have an integer called myNumber with the value 42, you write String.valueOf(myNumber) and get back "42" as text. This works for any number type: integers, decimals, booleans, even objects. It's built into Java's standard library and handles null values safely by turning them into the word "null".
The second method is Integer.toString(), which works only on integers. You write Integer.toString(myNumber) and get the same result. It's slightly faster than String.valueOf() because it does one less step behind the scenes, but the difference is too small to matter in real code. The tradeoff: if your variable is null, this method crashes instead of returning "null".
The third method is concatenation with an empty string. You write myNumber + "" and Java converts the number to text automatically. This is the shortest to type but the slowest to run, because Java creates a temporary object in memory to handle the concatenation. For a single conversion it doesn't matter. For thousands of conversions in a loop, it adds up.
Key Takeaways
- String.valueOf() is the safest choice for most situations because it handles null values and works with any data type.
- Integer.toString() is slightly faster than String.valueOf() but only works on integers and crashes if the value is null.
- Concatenating with an empty string (myNumber + "") works but is slower and should be avoided in loops or performance-critical code.
- If you need to format the number — add commas, control decimal places, or pad with zeros — use String.format() instead of these basic methods.
When to use String.valueOf() instead of Integer.toString()
Choose String.valueOf() when you don't know the exact type of the number you're converting, or when the variable might be null. It's the defensive choice. If your code receives a number from a database query, a user input field, or another part of your program, and you're not certain it will always have a value, String.valueOf() won't crash — it will return the string "null" instead.
Choose Integer.toString() when you know for certain the value is not null and you're working only with integers. It's slightly more efficient and makes your intent clearer to someone reading the code: you're saying "this is definitely an integer, and I want it as text." In performance-sensitive code — like converting thousands of numbers in a tight loop — the small speed difference can matter.
In practice, most Java programmers use String.valueOf() as their default because the performance difference is negligible in typical applications, and the safety is worth it.
Using String.format() when you need to control how the number looks
If you need the number to appear a certain way — with commas as thousand separators, a specific number of decimal places, or leading zeros — the basic conversion methods won't do it. Use String.format() instead.
To add commas to a large number, write String.format("%,d", myNumber). The %,d part tells Java "format this as a decimal integer with commas." So 1000000 becomes "1,000,000". To show a decimal number with exactly two decimal places, write String.format("%.2f", myDecimal). The %.2f means "format as a floating-point number with 2 digits after the decimal point." If your number is 19.5, it becomes "19.50".
To pad a number with leading zeros — useful for IDs or codes — write String.format("%05d", myNumber). The %05d means "format as a 5-digit integer, padding with zeros on the left." So 42 becomes "00042". You can combine these: String.format("%,d", myNumber) adds commas, and String.format("$%.2f", myDecimal) adds a dollar sign and two decimal places.
Why concatenation with an empty string works but shouldn't be your first choice
When you write myNumber + "", Java sees that you're trying to add a number and a string, which doesn't make sense. So it automatically converts the number to text first, then combines them. The result is the same as String.valueOf(myNumber), but the process is less direct.
Behind the scenes, Java creates a temporary StringBuilder object to handle the concatenation, then throws it away once the operation is done. For a single conversion, this overhead is invisible. But if you're converting 10,000 numbers in a loop, you're creating and discarding 10,000 temporary objects. That wastes memory and processor time.
The concatenation method is fine for one-off conversions in readable code — some programmers like it because it's obvious what's happening. But for any code that runs repeatedly or needs to be fast, use String.valueOf() or Integer.toString() instead.
Converting different number types: decimals, longs, and doubles
String.valueOf() works on all number types in Java. If you have a decimal number (called a double or float), write String.valueOf(myDecimal) and it becomes text. If you have a very large integer (called a long), the same method works. The conversion is automatic — Java figures out what type you're converting and handles it.
For decimals, be aware that the text version will show all the digits Java stored internally, which can sometimes be more than you want. If your decimal is 19.5, you might get "19.5" or "19.500000000001" depending on how the number was calculated. This is why String.format() is useful for decimals — it lets you control exactly how many decimal places appear in the final text.
If you're converting a long (a very large integer), String.valueOf() works just as well as for regular integers. There's also Long.toString() if you want the slightly faster option, but the same tradeoff applies: it crashes on null values.
What happens when you convert null, and how to handle it safely
If you try to convert a null value using String.valueOf(null), you get back the text "null" — not an error, just the word "null" as a string. This is safe but might not be what you want. If you're building a sentence or a data file, the word "null" appearing in the output looks like a mistake.
To handle null values the way you want, check for them before converting. Write if (myNumber != null) { String.valueOf(myNumber); } to only convert if the value exists. Or use a ternary operator: myNumber != null ? String.valueOf(myNumber) : "0" converts the number if it exists, or returns "0" if it doesn't.
If you use Integer.toString() on a null value, the program crashes with a NullPointerException. This is why String.valueOf() is safer for code that might receive null values from outside sources.
Frequently Asked Questions
What's the difference between String.valueOf() and toString()?
String.valueOf() is a static method that works on any type and handles null safely. The toString() method belongs to the object itself and crashes if called on null. For converting numbers, String.valueOf() is the better choice.
Can I convert a number to a string with a specific base, like binary or hexadecimal?
Yes. Use Integer.toBinaryString(myNumber) for binary, Integer.toHexString(myNumber) for hexadecimal, or Integer.toOctalString(myNumber) for octal. These methods are built into Java's Integer class.
Why does my decimal number show extra digits after conversion?
Computers store decimals as approximations, not exact values. When you convert to text, you see all the stored digits. Use String.format("%.2f", myDecimal) to round and control how many decimal places appear.
Is there a performance difference between these methods in real applications?
For single conversions, no. The difference only matters if you're converting thousands of numbers in a tight loop. In that case, Integer.toString() is slightly faster than String.valueOf(), and both are much faster than concatenation with an empty string.
What if I need to convert a number to text and save it to a file?
Convert it to text first using any of these methods, then write the text to the file. The conversion itself is separate from file writing. Most file-writing methods in Java accept strings directly, so String.valueOf(myNumber) gives you the text you need.