The three main ways to convert an int to a String

Java gives you three straightforward methods to turn an integer into a String. The simplest is String.valueOf(), which takes an int and returns its String equivalent in one line. The second is Integer.toString(), which does the same thing and is equally fast. The third is concatenation with an empty string, which works but is less readable and slightly slower in loops.

For most situations, use String.valueOf(myInt) or Integer.toString(myInt) — they are explicit about what you are doing and perform identically. Concatenation is fine for one-off conversions in simple code, but avoid it when you are converting many integers in a loop.

Key Takeaways

  • String.valueOf(42) and Integer.toString(42) are the two standard methods and produce identical results.
  • Concatenating with an empty string like "" + 42 works but is less clear and slower when converting many integers.
  • All three methods handle negative integers correctly without extra work on your part.
  • If you need to convert to a different base (binary, hexadecimal), use Integer.toBinaryString(), Integer.toHexString(), or Integer.toString(int, radix).

Using String.valueOf() for clarity

String.valueOf() is the most explicit method and reads clearly in code. You pass an int to the method and it returns a String. Here is the basic pattern:

int myNumber = 42; String result = String.valueOf(myNumber); // result is now "42"

This method also works with other data types — you can pass a double, a boolean, or a char and get back a String representation. That consistency makes it a safe choice when you are not sure what type you are converting. Many developers prefer String.valueOf() because it signals that you are converting something to a String, regardless of the input type.

Using Integer.toString() as an alternative

Integer.toString() does exactly what String.valueOf() does for integers, and the performance is identical. Some developers prefer it because it is more specific — the method name tells you it is converting an Integer, not just any object.

int myNumber = 42; String result = Integer.toString(myNumber); // result is now "42"

Pick whichever reads better to you and your team. There is no performance reason to choose one over the other. In practice, the Java compiler often treats them the same way internally.

String concatenation: quick but less readable

Concatenating your integer with an empty string works because Java automatically converts the int to a String during the operation:

int myNumber = 42; String result = "" + myNumber; // result is now "42"

This is fine for a single conversion in simple code. However, if you are converting many integers — say, in a loop that runs hundreds of times — the repeated string creation adds up. The Java compiler can optimize this in some cases, but String.valueOf() and Integer.toString() are clearer about your intent and consistently faster. Avoid concatenation in performance-sensitive code.

Converting to different number bases

If you need the binary or hexadecimal representation of an integer, Java provides dedicated methods. Integer.toBinaryString() returns the binary form, and Integer.toHexString() returns hexadecimal:

int myNumber = 42; String binary = Integer.toBinaryString(myNumber); // binary is now "101010" String hex = Integer.toHexString(myNumber); // hex is now "2a"

For other bases, use Integer.toString(int, radix), where radix is the base you want (2 through 36). For example, base 16 is hexadecimal and base 2 is binary:

int myNumber = 42; String base16 = Integer.toString(myNumber, 16); // base16 is now "2a" String base8 = Integer.toString(myNumber, 8); // base8 is now "52"

Handling negative integers and edge cases

All three methods handle negative integers without any special handling. The minus sign is included in the resulting String:

int negativeNumber = -42; String result = String.valueOf(negativeNumber); // result is now "-42"

This works the same way whether you use String.valueOf(), Integer.toString(), or concatenation. The conversion preserves the sign automatically. For the boundary values Integer.MAX_VALUE (2147483647) and Integer.MIN_VALUE (-2147483648), all three methods convert correctly without data loss, since a String can hold any number of digits.

When to use each method in real code

Use String.valueOf() when you are writing general-purpose code that might convert different types, or when you want the clearest signal that a conversion is happening. Use Integer.toString() when you are specifically working with integers and want the method name to reflect that. Use concatenation only in throwaway code or when converting a single integer in a context where clarity is less important than brevity.

In production code, avoid concatenation in loops or anywhere performance matters. If you are building a String from many integers, consider using a StringBuilder with repeated calls to String.valueOf() or Integer.toString(), which is faster than repeated concatenation.

Frequently Asked Questions

What is the difference between String.valueOf() and Integer.toString()?

They produce identical results for integers and have the same performance. String.valueOf() is more general — it works with any type — while Integer.toString() is specific to integers. Choose based on readability and what your code is doing.

Is concatenation with an empty string slower?

For a single conversion, the difference is negligible. In a loop converting hundreds or thousands of integers, String.valueOf() and Integer.toString() are measurably faster. Use concatenation only when converting one or two integers in simple code.

Can I convert a String back to an int?

Yes, use Integer.parseInt(). For example, int result = Integer.parseInt("42"); converts the String "42" back to the integer 42. Be aware that parseInt() throws an exception if the String does not contain a valid integer.

What happens if I convert Integer.MAX_VALUE or Integer.MIN_VALUE?

Both convert correctly to their String representations. String.valueOf(Integer.MAX_VALUE) returns "2147483647" and String.valueOf(Integer.MIN_VALUE) returns "-2147483648". No data is lost because the String can hold any number of digits.

How do I convert an int to a String with leading zeros?

Use String.format() with a format specifier. For example, String result = String.format("%05d", 42); produces "00042" — a five-character String with leading zeros. The number after the % sign controls the total width.