Convert an Integer to a String in Java
This post will discuss how to convert an integer to a string in Java. If the value of the specified integer is negative, the solution will preserve the sign in the resultant string.
There are many instances where we need to convert int, double, or float values into a string (and vice-versa). Conversion to string is also needed to prevent arithmetic overflow—for example, the 2147483648 number represents Integer.MAX_VALUE + 1 is too large to store in an int data type and throws an error, but we can store the same value in a String object as "2147483648".
There are many ways to convert an integer to a string in Java:
1. Using String.valueOf() method
String class provides a valueOf() static method that returns the string representation of the specified integer argument.
|
1 2 3 |
int n = Integer.MAX_VALUE; String str = String.valueOf(n); System.out.println(str); |
2. Using Integer.toString() method
Integer class toString() can be used for conversion from integer to string. It has two overloaded versions, as shown below:
|
1 2 3 |
int n = Integer.MAX_VALUE; String str = Integer.toString(n); System.out.println(str); |
|
1 2 3 |
int n = Integer.MAX_VALUE; String str = new Integer(n).toString(); System.out.println(str); |
3. Using String.format() method
String format() returns a formatted string that can convert an integer to a string.
|
1 2 3 |
int n = Integer.MAX_VALUE; String str = String.format("%d", n); System.out.println(str); |
4. Using DecimalFormat.format() method
DecimalFormat class provides a variety of features to parse and format numbers. We can use it in many ways to format an integer to produce a string, as shown below:
|
1 2 3 |
int n = Integer.MAX_VALUE; String str = new DecimalFormat().format(n); System.out.println(str); // prints 2,147,483,647 |
|
1 2 3 |
int n = Integer.MAX_VALUE; String str = new DecimalFormat("#").format(n); System.out.println(str); // prints 2147483647 |
5. Using StringBuffer or StringBuilder
StringBuilder class append() method is overloaded to accept data of any type. We can use it for conversion from integer to string, as shown below:
|
1 2 3 |
int n = Integer.MAX_VALUE; String str = new StringBuilder().append(n).toString(); System.out.println(str); |
StringBuffer works similarly but is thread-safe and hence slower.
6. String Concatenation
We can use the expression "" + n to convert an integer n to a string.
|
1 2 3 |
int n = Integer.MAX_VALUE; String str = "" + n; System.out.println(str); |
This approach is unconventional, and the above code will translate to the following code at runtime:
|
1 |
String str = new StringBuilder().append("").append(n).toString(); |
That’s all about converting an Integer to a Java String.
Related Post:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)