This post will look at various ways of converting an int to a String in Java.
Below are some of the many ways of converting an int to a String –
- Integer.toString() method
- String.ValueOf() method
- Using + operator
- Using StringBuffer or StringBuilder append() function
- String.format() method
Let’s look at all of the ways one by one.
Using toString() method
Java has an Integer wrapper class so that we can perform class operations on the int primitive type, and that Integer class has a static toString() method which will take an int as an argument and return a String object. Below is the signature of the method –
public static String toString(int i)
We can use the below line to convert an int to a string.
String str = Integer.toString(i); // where i is an integer
The whole program of converting an int to a String is shown below.
public class Codekru {
public static void main(String[] args) {
int i = 205;
String str = Integer.toString(i);
System.out.println(str instanceof String); // will print true
System.out.println(str); // will print "205"
}
}
Output –
true
205
Using String.ValueOf() method
String class has a static valueOf() method, which will take an int as an argument and return a String object.
public static String valueOf(int i)
We will let you on a little secret, String .valueOf() method internally uses Integer.toString() method.
Below is the internal implementation of the String.valueOf() function, which takes an int as an argument and converts it into a String.
public static String valueOf(int i) {
return Integer.toString(i);
}
And Below is the example of converting an int to a String using the valueOf() method.
public class Codekru {
public static void main(String[] args) {
int i = 205;
String str = String.valueOf(i);
System.out.println(str instanceof String); // will print true
System.out.println(str); // will print "205"
}
}
Output –
true
205
Using + operator
Do you know we can directly convert an int to a String using the + operator?
public class Codekru {
public static void main(String[] args) {
int i = 205;
int j = 206;
String str = "";
str = str + i + j; // acting as a string concatenation operator here
System.out.println(str instanceof String); // will print true
System.out.println(str); // will print "205206"
}
}
Output
true
205206
Using StringBuffer or StringBuilder append() function
We can also use the append() function in StringBuilder and StringBuffer classes to convert an int to a String. The below code helps us achieve the same.
public class Codekru {
public static void main(String[] args) {
int i = 205;
String str = new StringBuffer().append(i).toString();
System.out.println(str instanceof String); // will print true
System.out.println(str); // will print "205"
}
}
Output –
true
205
String.format() method
We can also use the String class’s static format() method.
public class Codekru {
public static void main(String[] args) {
int i = 1234;
String str = String.format("%d", i);
System.out.println(str);
}
}
Output –
1234
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.