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.

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:

3. Using String.format() method

String format() returns a formatted string that can convert an integer to a string.

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:

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:

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.

 
This approach is unconventional, and the above code will translate to the following code at runtime:

That’s all about converting an Integer to a Java String.

 
Related Post:

Convert a String to an Integer in Java