Java - Convert integer to string
Categories:
Converting Integers to Strings in Java: A Comprehensive Guide

Learn the various methods to convert integer primitive types and Integer objects to String representations in Java, understanding their nuances and best use cases.
Converting an integer to its string representation is a common task in Java programming. Whether you're displaying numerical data to a user, logging values, or performing string manipulations, understanding the different conversion methods is crucial. This article explores the most common and efficient ways to achieve this, covering both primitive int
types and Integer
objects.
Why Convert Integers to Strings?
The primary reason for converting an integer to a string is to enable operations that are specific to strings, such as concatenation with other text, formatting for display, or parsing for specific patterns. Integers are numerical values used for mathematical operations, while strings are sequences of characters. Java provides several built-in mechanisms to bridge this gap seamlessly.
flowchart TD A[Integer Value] --> B{"Need String Representation?"} B -- Yes --> C[Conversion Methods] C --> D1[String.valueOf()] C --> D2[Integer.toString()] C --> D3[Concatenation with ""] C --> D4[DecimalFormat] D1 --> E[String Output] D2 --> E D3 --> E D4 --> E B -- No --> F[Perform Math Operations]
Decision flow for integer to string conversion in Java.
Method 1: Using String.valueOf()
The String.valueOf()
method is a versatile and commonly used approach. It's overloaded to accept various primitive types and objects, making it a general-purpose conversion utility. When passed an int
or Integer
, it returns its string representation.
public class StringValueOfExample {
public static void main(String[] args) {
int primitiveInt = 123;
Integer integerObject = 456;
String str1 = String.valueOf(primitiveInt);
String str2 = String.valueOf(integerObject);
System.out.println("Primitive int to String: " + str1); // Output: Primitive int to String: 123
System.out.println("Integer object to String: " + str2); // Output: Integer object to String: 456
}
}
Converting int
and Integer
to String
using String.valueOf()
.
String.valueOf()
is generally preferred for its readability and ability to handle null
objects gracefully (it returns the string "null" for a null
object, avoiding NullPointerException
).Method 2: Using Integer.toString()
The Integer.toString()
method is a static method of the Integer
wrapper class. It specifically converts an int
primitive or an Integer
object into its string representation. It's functionally similar to String.valueOf()
when dealing with integers, but it's more specific to numerical types.
public class IntegerToStringExample {
public static void main(String[] args) {
int primitiveInt = 789;
Integer integerObject = 1011;
String str1 = Integer.toString(primitiveInt);
String str2 = integerObject.toString(); // Instance method call
System.out.println("Primitive int to String: " + str1); // Output: Primitive int to String: 789
System.out.println("Integer object to String: " + str2); // Output: Integer object to String: 1011
}
}
Converting int
and Integer
to String
using Integer.toString()
.
integerObject.toString()
, if integerObject
is null
, it will result in a NullPointerException
. This is a key difference compared to String.valueOf(null)
.Method 3: String Concatenation with an Empty String
A simple and often used trick is to concatenate the integer with an empty string (""
). Java's string concatenation operator (+
) automatically converts the non-string operand to a string before performing the concatenation. This method is concise but can sometimes be less explicit about the conversion's intent.
public class ConcatenationExample {
public static void main(String[] args) {
int number = 12345;
String str = number + "";
System.out.println("Concatenated String: " + str); // Output: Concatenated String: 12345
}
}
Converting an int
to String
using string concatenation.
String
objects. For such scenarios, StringBuilder
or StringBuffer
are more appropriate.Method 4: Using DecimalFormat
for Formatted Output
If you need to format the integer with specific patterns (e.g., leading zeros, grouping separators), java.text.DecimalFormat
is the tool for the job. This method provides much greater control over the output string's appearance.
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
int number = 12345;
DecimalFormat formatter = new DecimalFormat("000000"); // Format with 6 digits, leading zeros
String formattedString = formatter.format(number);
System.out.println("Formatted String: " + formattedString); // Output: Formatted String: 012345
int largeNumber = 1234567;
DecimalFormat commaFormatter = new DecimalFormat("#,###"); // Format with comma separator
String commaFormattedString = commaFormatter.format(largeNumber);
System.out.println("Comma Formatted String: " + commaFormattedString); // Output: Comma Formatted String: 1,234,567
}
}
Converting and formatting an int
to String
using DecimalFormat
.
String.format()
or System.out.printf()
can also be used, offering C-style formatting capabilities (e.g., String.format("%06d", number)
).