Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java int to String: Choosing the Right Conversion Method

Learn the differences between Integer.toString, String.valueOf, and concatenation for converting int to String in Java, including edge cases and performance.

~5 min readJava
#Java#String Conversion#Integer#Java Basics
Diagram showing Java int to String conversion methods with code snippets

Converting an int to a String is a common Java operation. The main methods are Integer.toString(int), String.valueOf(int), and string concatenation with +. For a primitive int, all three produce the same string. They differ mainly in null handling, readability, and allocation.

The Core Conversion APIs

The Java standard library provides three primary ways to convert an int to a String: the static method Integer.toString(int), the overloaded String.valueOf(int), and the string concatenation operator +. Each approach produces the same result for a non-null int value, but the underlying mechanics differ.

java
int number = 42; String fromInteger = Integer.toString(number); String fromValueOf = String.valueOf(number); String fromConcat = "" + number;

All three statements assign the string "42" to their respective variables. The first two are explicit conversion calls; the third uses Java's string concatenation mechanism. For a primitive int, the result is identical, but the code you write signals intent differently to future maintainers.

Integer.toString(int) and Its Behavior

Integer.toString(int) is a static method that takes an int primitive and returns a String representation using base 10 by default. It also has an overload that accepts a radix, so you can request binary, octal, or hexadecimal output.

java
int value = 255; String decimal = Integer.toString(value); // "255" String binary = Integer.toString(value, 2); // "11111111" String hex = Integer.toString(value, 16); // "ff"

The radix overload is useful when you need a non-decimal representation. For the common case of decimal conversion, the single-argument version is sufficient. This method does not handle null because an int primitive cannot be null; it throws a NullPointerException only if you pass an Integer object that is null, which is a common pitfall when mixing primitives and wrappers.

String.valueOf(int) and Autoboxing

String.valueOf(int) is an overloaded method for primitive values, including int. For int, it delegates to Integer.toString, so the output is identical. The main difference is that String.valueOf is also overloaded for Object, which means if you pass an Integer reference, the behavior changes.

java
Integer boxed = null; String result = String.valueOf(boxed); // "null"

This is a critical distinction. When you call String.valueOf with an Integer object, the compiler selects the Object overload, which returns the string "null" for a null reference. In contrast, Integer.toString(boxed) throws a NullPointerException because it unboxes the Integer to int. This behavior often surprises developers who assume both methods handle null identically.

Concatenation with an Empty String

Using "" + number is a common idiom that relies on Java's string concatenation semantics. For a primitive int, it produces the same string as String.valueOf(number). Before Java 9, the compiler typically transformed concatenation into a StringBuilder append operation; since Java 9, concatenation is compiled through StringConcatFactory, so the exact implementation is delegated to the runtime. The expression is concise and readable in many contexts, especially when building longer strings.

java
int count = 7; String message = "Total: " + count;

The concatenation approach is convenient for inline formatting, but the resulting allocation behavior depends on the Java version and compilation strategy. Explicit methods are clearer when the conversion is the sole purpose of the expression. When concatenating object references, a null operand is rendered as "null", which can hide bugs; a primitive int operand cannot be null.

Formatting Conversions with String.format and DecimalFormat

When you need more control over the output, such as leading zeros or locale-specific formatting, String.format and DecimalFormat offer additional options. These are not simple int-to-string conversions; they produce formatted strings from numeric values.

java
int value = 42; String padded = String.format("%04d", value); // "0042"

DecimalFormat allows pattern-based formatting (the example assumes java.text.DecimalFormat is imported):

java
DecimalFormat df = new DecimalFormat("#,##0"); String formatted = df.format(1234567); // "1,234,567"

These methods are appropriate when the output must match a specific format. They do more work than a plain conversion, such as interpreting the format pattern and applying locale conventions. For plain decimal conversion, they are overkill and can introduce performance costs in high-throughput code.

Handling Null and Edge Cases

The most common edge case in int-to-string conversion is the presence of null when using wrapper types. A primitive int can never be null, but an Integer can. The behavior differs across methods:

Method or expressionNull Integer behavior
Integer.toString(int) with a null Integer argumentThrows NullPointerException during unboxing
String.valueOf(Integer)Returns "null"
"" + IntegerReturns "null"

Numeric formatting methods such as String.format("%d", value) and DecimalFormat.format(value) also require a non-null numeric value; guard them with an explicit null check before formatting.

If your code receives an Integer from a database or a deserialization framework, you must decide how to handle null. Using String.valueOf may silently produce the string "null", which can propagate incorrect data. Explicitly checking for null before conversion is often safer, especially when the result is used in logging or user-facing messages.

Negative numbers behave consistently across all methods; they include the minus sign. The radix-based Integer.toString also handles negative numbers correctly, producing a leading minus sign followed by the magnitude in the specified base.

Performance and Allocation Considerations

For a single conversion, the performance difference between Integer.toString and String.valueOf is negligible because String.valueOf delegates to Integer.toString internally. The real performance concern arises when converting many ints in a loop.

java
// Inefficient: creates a new String for each iteration String result = ""; for (int i = 0; i < 1000; i++) { result += i; }

This loop creates a new StringBuilder and String for each iteration, leading to O(n^2) copying. Using an explicit StringBuilder avoids that overhead:

java
StringBuilder sb = new StringBuilder(); for (int i = 0; i < 1000; i++) { sb.append(i); } String result = sb.toString();

When the conversion is part of a larger string assembly, append the int directly to a StringBuilder rather than converting it first. StringBuilder.append(int) writes the digits directly into the builder's internal buffer, so there is no benefit to pre-converting.

Choosing the Right Approach

Select the conversion method based on the context and the likelihood of null values. Use Integer.toString when you are certain the value is a primitive int or a non-null Integer, and you want explicit intent. Use String.valueOf when you need to handle a potentially null Integer without throwing an exception, but be aware of the "null" result. Avoid concatenation with an empty string when you only need the string representation; the explicit methods are clearer. Reserve String.format and DecimalFormat for cases where formatting rules are required.

For code that processes user input or external data, always validate for null before conversion. The silent behavior of String.valueOf can mask data-quality issues that should be caught early. In performance-sensitive sections, reuse a StringBuilder and append ints directly to minimize allocation. The right choice balances readability, edge-case handling, and runtime cost.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.