software-development

How to Convert an int to a String in Java

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 repres...

Mara Ellison
How to Convert an int to a String in Java

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 a String, even for null (returns "null").
  • Integer.toString(int): Slightly more direct, avoids a potential null check edge case, and is exactly equivalent to the static conversion applied to the primitive.
  • String concatenation: Leverages compiler translation of + to StringBuilder, 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 intentional

Comparison 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 Integer with primitive int: passing a potentially null Integer to Integer.toString(Integer) can cause NullPointerException; prefer String.valueOf(Integer) when null safety is needed.
  • Using concatenation in performance-critical loops: each "" + value can allocate a new StringBuilder; reuse StringBuilder or prefer explicit conversion.
  • Assuming locale-specific formatting: conversion produces decimal digits consistent across locales; only formatting APIs like NumberFormat introduce 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 be Integer and you want safe null-to-"null" behavior.
  • Integer.toString(int) when the source is strictly primitive and you want an explicit, minimal conversion.
  • Explicit StringBuilder when 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.

Related Reading

More pages in this topic cluster.

How to Make Minecraft Plugins: A Verified Technical Guide

Making a Minecraft plugin means writing server side code that hooks into the Minecraft server software to change or extend gameplay, commands, data, and integrations. Unlike mod...

Read next
Sprint Dirt: What It Is, Why It Happens, and How to Manage It

Sprint dirt is the accumulation of small, often invisible issues that slow teams down across a sprint—unclear requirements, brittle tests, flaky environments, and handoff fric...

Read next
Understanding Chandler Garbage Collection in Computing

In computing, garbage collection is an automatic memory management mechanism that reclaims unused objects to free resources. In the context of the Chandler information manager,...

Read next