Quick Answer
In Java, the most idiomatic ways to convert an int to a String are String.valueOf(int) and Integer.toString(int). Both are clear, efficient, and produce the same textual representation of the integer. For quick debugging, string concatenation with an empty string also works, but it can be less clear and slightly less efficient. This guide explains each approach with examples, helping you choose the right pattern for readability, performance, and compatibility.
Why int to String Conversion Matters
Converting primitive int values to String is common when building messages, writing text-based output, serializing data, or concatenating for logging. Java does not automatically treat an int as a String, so explicit conversion is required. Choosing a consistent, idiomatic pattern improves readability and reduces bugs across your codebase.
Primary Methods Overview
Three standard approaches cover nearly all use cases:
String.valueOf(int): Readable and consistently returns aString, even fornull(returns"null").Integer.toString(int): Slightly more direct, avoids a potentialnullcheck edge case, and is exactly equivalent to the static conversion applied to the primitive.- String concatenation: Leverages compiler translation of
+toStringBuilder, which can be convenient but may obscure intent and perform extra work in tight loops.
Method 1: Using String.valueOf(int)
String.valueOf(int) is a static, null-safe method that returns the decimal representation of the argument. Its signature is public static String valueOf(int i). When called on a primitive int, it cannot be null; when called on an Integer object, a null input yields the string "null". This makes it convenient when the source might be a boxed type.
Signature and Internals
The JVM implements String.valueOf(int) by calling Integer.toString(int) internally. There is negligible overhead, making it perfectly suitable for production code where clarity is preferred. Use this when you also want consistent behavior for possible Integer inputs.
Examples
Basic usage:
int count = 42;
String asString = String.valueOf(count);
// asString == "42"Using null with the boxed type:
Integer maybeNumber = null;
String text = String.valueOf(maybeNumber);
// text == "null"Method 2: Using Integer.toString(int)
Integer.toString(int i) is the direct, static method on the wrapper class. It is semantically the same as String.valueOf(int) for primitives, but does not define special handling for a null object because it accepts a primitive. Some teams prefer it because it explicitly signals that you are converting an integer to its string form.
Signature and Behavior
Signature: public static String toString(int i). It always returns a non-null string for primitive input and is consistent across Java versions. Throwaways or temporary conversions can be written concisely as Integer.toString(number).
Examples
int version = 17;
String label = Integer.toString(version);
// label == "17"Method 3: Using String Concatenation
Java compiles string concatenation using StringBuilder (or StringBuffer in certain contexts). Writing "" + number works, but it creates an extra object and can be less clear about intent. It is acceptable for quick debugging or scripts, but prefer explicit methods in production code.
Example and Caveats
int port = 8080;
String viaConcat = "" + port;
// viaConcat == "8080", but less intentionalComparison at a Glance
| Method | Null Handling (Integer input) | Performance (relative) | Readability |
|---|---|---|---|
| String.valueOf(int) | Returns "null" for null input | Very low overhead (wraps Integer.toString) | Clear and safe for mixed inputs |
| Integer.toString(int) | Accepts primitive only; no null input | Very low overhead | Explicit, intent-focused |
| "" + int | Boxes int, uses StringBuilder | Slightly higher overhead | Concise but ambiguous at scale |
Java Version Considerations
All three methods have existed since early Java versions and remain stable through current releases (Java 17, Java 21, and beyond). There are no deprecated behaviors around int-to-String conversion. If you use newer formatting features like String.format or text blocks for complex output, they are built on the same underlying conversion routines.
Formatting and Base Variations
If you need non-decimal representations (hex, octal, binary), use Integer.toString(int i, int radix). For example, Integer.toString(255, 16) returns "ff". This is helpful for low-level protocols, color values, or bitmask debugging. For formatted output, String.format also delegates to the same conversion logic internally.
Radix Examples
int flags = 255;
String hex = Integer.toString(flags, 16); // "ff"
String binary = Integer.toString(flags, 2); // "11111111"
String octal = Integer.toString(flags, 8); // "377"Common Pitfalls and How to Avoid Them
- Confusing boxed
Integerwith primitiveint: passing a potentially nullIntegertoInteger.toString(Integer)can causeNullPointerException; preferString.valueOf(Integer)when null safety is needed. - Using concatenation in performance-critical loops: each
"" + valuecan allocate a newStringBuilder; reuseStringBuilderor prefer explicit conversion. - Assuming locale-specific formatting: conversion produces decimal digits consistent across locales; only formatting APIs like
NumberFormatintroduce locale-specific behavior.
Performance and Best Practices
For typical application code, the performance difference between String.valueOf(int) and Integer.toString(int) is negligible. In hot paths, prefer clarity and consistency. For building multiple pieces into a single string, use an explicit StringBuilder rather than repeated concatenation to avoid hidden allocations.
When to Use Each Method
Choose based on context:
String.valueOf(int)when handling inputs that might beIntegerand you want safe null-to-"null" behavior.Integer.toString(int)when the source is strictly primitive and you want an explicit, minimal conversion.- Explicit
StringBuilderwhen concatenating many pieces or in performance-sensitive loops. - Radix variants when you need hex, binary, or octal output.
Summary
Converting int to String in Java is straightforward and stable. Use String.valueOf(int) for the most flexible, null-safe behavior or Integer.toString(int) for a direct, primitive-only call. Avoid relying on string concatenation for production conversion logic. With these patterns you can handle conversion needs cleanly across Java versions and contexts.