Convert int to String in Java
In Java, you can convert a primitive int value to a String using Integer.toString() or String.valueOf(). For example, the integer 5239 becomes the string "5239", while -25 becomes "-25".
String concatenation and StringBuffer.append() can also produce the string representation of an integer. This tutorial shows each approach with Java programs and explains when each form is appropriate.
Java int-to-String syntax
The two direct methods for converting an int to a String are:
String str1 = Integer.toString(number);
String str2 = String.valueOf(number);
Both methods return the decimal string representation of the integer. The sign is preserved for negative values.
1. Convert int to string using Integer.toString() method
Integer.toString(int) is a static method of the Integer class. It returns a String representing the specified integer in decimal form.
Pass the integer value as the argument to Integer.toString(). Positive, zero, and negative int values can all be converted this way.
In the following example we shall convert an int value (either true or false) to string by using Integer.toString() method.
Java Program
/**
* Java Program - Convert Int to String
*/
public class IntToString {
public static void main(String[] args) {
String str = Integer.toString(38426);
System.out.println(str);
str = Integer.toString(-938426);
System.out.println(str);
}
}
Output
38426
-938426
The first call returns "38426". The second returns "-938426", including the minus sign.
2. Convert int to string using String Concatenation
An integer is converted to its text representation when it is concatenated with a String. Therefore, adding an empty string to an integer produces a String.
In the following example we shall convert an integer to string by concatenating the number to an empty string.
Java Program
/**
* Java Program - Convert Int to String
*/
public class IntToString {
public static void main(String[] args) {
int n = 5239;
//convert int to string using string concatenation
String str = n + "";
System.out.println(str);
}
}
Run the above program and you shall get the following output.
Output
5239
The expression n + "" works, but Integer.toString(n) or String.valueOf(n) is usually clearer when the only purpose is to convert an integer to a string. Concatenation is more natural when the number is part of a larger message.
int quantity = 12;
String message = "Quantity: " + quantity;
System.out.println(message);
Quantity: 12
3. Convert int to string using String.valueOf() method
String.valueOf(int) returns the string representation of an integer. Pass the int value as the argument, and the method returns a String.
In the following example we shall convert an integer to string by using String.valueOf() function.
Java Program
/**
* Java Program - Convert Int to String
*/
public class IntToString {
public static void main(String[] args) {
int n = 5239;
//convert int to string using String methods
String str = String.valueOf(n);
System.out.println(str);
}
}
The value stored in str is "5239". Although its characters look like the original number, its Java type is now String.
5239
4. Convert int to string using StringBuffer.append() method
StringBuffer.append(int) appends the decimal representation of an integer to the character sequence. Calling toString() on the resulting StringBuffer returns a String.
You can create the StringBuffer, append the integer, and call toString() in one chained expression. This approach is most useful when a string is already being assembled from several values.
In the following example we shall convert a integer to string by using StringBuilder class.
Java Program
/**
* Java Program - Convert Int to String
*/
public class IntToString {
public static void main(String[] args) {
String str = (new StringBuffer()).append(2638).toString();
System.out.println(str);
str = (new StringBuffer()).append(-8674).toString();
System.out.println(str);
}
}
Output
2638
-8674
Convert int to String using StringBuilder
StringBuilder supports the same basic pattern when synchronization provided by StringBuffer is not needed. Append the integer and then call toString().
int number = 4821;
String str = new StringBuilder().append(number).toString();
System.out.println(str);
4821
For converting only one integer, creating a StringBuilder or StringBuffer is unnecessary. These classes are more useful when multiple values are being appended while constructing a larger string.
Convert zero and negative int values to String in Java
The standard conversion methods work with the full range of Java int values. Zero becomes "0", and a negative integer keeps its minus sign.
int zero = 0;
int negative = -150;
String a = Integer.toString(zero);
String b = String.valueOf(negative);
System.out.println(a);
System.out.println(b);
0
-150
Convert an int to binary, hexadecimal, or another radix String
Integer.toString(int, int) can convert an integer to a string using a radix other than decimal. The second argument specifies the radix.
String str = Integer.toString(number, radix);
For example, radix 2 produces a binary representation and radix 16 produces a hexadecimal representation.
int number = 26;
String decimal = Integer.toString(number);
String binary = Integer.toString(number, 2);
String hexadecimal = Integer.toString(number, 16);
System.out.println(decimal);
System.out.println(binary);
System.out.println(hexadecimal);
26
11010
1a
If you simply need the usual decimal form of an integer, use the one-argument Integer.toString(number) method.
Integer.toString() vs String.valueOf() for int conversion
| Java approach | Example | Result | When to use it |
|---|---|---|---|
Integer.toString(n) | Integer.toString(25) | "25" | Direct and explicit conversion from int to String |
String.valueOf(n) | String.valueOf(25) | "25" | General-purpose conversion to String |
| String concatenation | 25 + "" | "25" | Useful when the integer is already part of a larger string expression |
StringBuilder or StringBuffer | builder.append(25).toString() | "25" | Useful while constructing a string from multiple pieces |
For a straightforward int-to-String conversion, Integer.toString() and String.valueOf() are the clearest choices. Both produce the same decimal text for a primitive int.
Int-to-String conversion does not change the numeric value
After conversion, the result is text rather than a number. This affects later operations. For example, adding two integers performs arithmetic, while concatenating their string representations joins their characters.
int a = 12;
int b = 5;
System.out.println(a + b);
System.out.println(Integer.toString(a) + Integer.toString(b));
17
125
The first expression adds the integers. The second joins two strings. This distinction is useful when the converted value will be used in further expressions.
Choosing a Java int-to-String conversion method
Use Integer.toString(n) when you want an explicit integer conversion, or String.valueOf(n) when you prefer Java’s general value-to-string method. Both are appropriate for normal decimal conversion.
Use string concatenation when the integer is naturally being inserted into other text. Use StringBuilder or StringBuffer when you are already constructing a longer string from several pieces. If you need binary, hexadecimal, or another base, use the radix form of Integer.toString().
In this Java Tutorial, we learned how to convert an int to String in Java using Integer.toString(), String.valueOf(), string concatenation, StringBuffer, and StringBuilder.
TutorialKart.com