Converting an int to a String in Java is a common task with multiple reliable approaches. The most idiomatic and readable options are String.valueOf(int) and Integer.toString(int), both introduced long ago and stable since Java 1.0. You can also leverage string concatenation with an empty string, though it is less explicit. This article explains each method with verified examples, version considerations, performance notes, and best-practice guidance to help you choose the right pattern for production code.
Why int to String Conversion Is Common in Java
Integers and strings serve distinct roles: int is a primitive numeric type used for arithmetic, while String is an immutable sequence of characters meant for text representation, storage, and I/O. You routinely need to bridge these types for logging, UI labels, serialization, HTTP parameters, or concatenating messages. Because this conversion is ubiquitous and foundational, Java provides several clear, stable APIs to perform it safely across all mainstream versions.
Primary Methods to Convert int to String
String.valueOf(int)
String.valueOf(int) is widely favored for readability and null safety. When you pass an int primitive, it internally calls Integer.toString(int) and returns the resulting string. The method is concise and communicates intent clearly. It also provides overloads for other primitive types, which can be convenient in generic utility code.
Integer.toString(int)
Integer.toString(int) is the direct, explicit conversion from a primitive int to its String representation. It avoids unnecessary object boxing and is the canonical method defined on the wrapper class. Because it accepts only an int, it avoids ambiguity and is slightly more performant than alternatives that involve object handling.
String Concatenation
Java’s compiler implicitly converts int to String during concatenation with the + operator. Writing "" + number triggers this conversion, producing a String result. While convenient in quick scripts, this pattern is less explicit and can obscure intent in larger codebases. It may also introduce minor overhead due to the creation of intermediate StringBuilder objects.
Code Examples and Output
All examples below use only standard JDK APIs and remain compatible with Java 8 and later. Each approach yields the same textual representation of the integer, with no loss of precision.
| Method | Code Example | Result |
|---|---|---|
| String.valueOf(int) | String s = String.valueOf(42); | "42" |
| Integer.toString(int) | String s = Integer.toString(42); | "42" |
| Concatenation | String s = "" + 42; | "42" |
Handling null and Edge Cases
When converting an Integer object rather than a primitive int, null handling becomes important. Calling String.valueOf(Integer) on a null reference produces the literal string "null", which is often surprising. In contrast, Integer.toString(Integer) would throw a NullPointerException because it expects a primitive int. Use explicit null checks or Objects.toString(Object, String) if you require a placeholder for absent values rather than the string "null".
Performance and Memory Considerations
On the HotSpot JVM, String.valueOf(int) and Integer.toString(int) exhibit nearly identical performance for straightforward conversions. Both ultimately invoke the same internal logic to create the character array and construct the String. The concatenation approach, while syntactically brief, can allocate additional objects and is best reserved for ad hoc use. For high-throughput or low-latency code, prefer the explicit methods and reuse buffers when converting in loops.
Best Practices and Recommendations
- Prefer
String.valueOf(int)for general use due to clarity and null-safety with boxed types. - Use
Integer.toString(int)when you want to emphasize the primitive nature of the source and avoid subtle autoboxing. - Avoid
"" + numberin performance-sensitive paths or large codebases where explicitness matters. - Validate or handle null inputs explicitly before conversion if your domain requires special sentinel values or error reporting.
Compatibility Across JVM Versions
The APIs discussed here are part of the core Java standard library since Java 1.0 and remain unchanged in behavior across Java 8, 11, 17, and later long-term support releases. You can rely on consistent encoding, default decimal formatting, and exception behavior without adding external libraries. Future Java versions are expected to maintain these methods for backward compatibility, making them evergreen choices for int-to-string conversion.