
There are a few ways to convert an integer to String in Java. In this article we are comparing these methods with simple code samples.
If there were no conversion methods, we would have to parse the digits of an integer ourselves. Fortunately, standard Java library helps us with simple conversion methods.
1) Integer.toString()
If you have a primitive int value, this is the preferred method to convert it to a String object. Integer.toString()
is a static method, so you don’t have to create an Integer instance.
There are some small variations between Java implementations. In OpenJDK implementation, int value is converted to byte array and a new String object is created from this array. Integer.toString()
method in Java library uses a very efficient method in terms of performance.
public class ConversionDemo1 {
public static void main(String[] args) {
int num = 20;
String str1 = Integer.toString(num);
String str2 = Integer.toString(-40);
System.out.println("str1: " + str1 + ", str2: " + str2);
// str1: 20, str2: -40
}
}
2) String.valueOf()
Compared with previous method, this method call has a similar performance. Static valueOf method of String class just calls Integer.toString()
method. So you can either use this method or Integer.toString()
method, if you want to convert a primitive int value.
For numeric types other than int (like long, float, double), String class offers overloaded valueOf() methods.
- public static String valueOf(long l)
- public static String valueOf(float f)
- public static String valueOf(double d)
public class ConversionDemo2 {
public static void main(String[] args) {
int num = 20;
String str1 = String.valueOf(num);
String str2 = String.valueOf(-40);
System.out.println("str1: " + str1 + ", str2: " + str2);
// str1: 20, str2: -40
}
}
3) toString() method of Integer class
If you already have an instance of Integer class, you can use toString() instance method.
public class ConversionDemo3 {
public static void main(String[] args) {
Integer obj = 20;
String str = obj.toString();
System.out.println("str: " + str);
// str: 20
}
}
4) String.format()
Using String.format() is also possible to convert an integer to a String but it is not a preferred method. String.format()
creates and uses many objects in memory (like Formatter, FormatString, Pattern, Matcher, …). Integer.toString()
or String.valueOf()
methods are more efficient.
public class ConversionDemo4 {
public static void main(String[] args) {
int num = 20;
String str = String.format("%d", num);
System.out.println("str: " + str);
// str: 20
}
}
5) Concatenating with an Empty String
In some examples, you can see that an integer is converted to a String by concatenating the int variable with an empty string.
Java compiler converts + operator to StringBuilder.append()
call when Java code is compiled into byte code. If you want to view the generated the byte code of a *.class file, You can disassemble it using javap -c
command.
This is not a recommended method in terms of performance either. Because 1- Unnecessary objects are created (Empty string and StringBuilder); 2- Integer.toString()
is a much simpler method;
public class ConversionDemo4 {
public static void main(String[] args) {
int num = 20;
String str = "" + num;
System.out.println("str: " + str);
// str: 20
}
}