There are many ways to convert an integer value (primitive int or an Integer object) into a string in Java. Unlike string to integer conversion, converting an integer to a string is a simple operation.

Integer.toString() method

The toString() method is the most common method provided by every Java object that returns a string representation of the object.

It can be used a static method of the Integer class to convert a primitive int value into a string:

int number = 2344;
String str = Integer.toString(number);
System.out.println(str);    // 2344

If the integer variable is already an instance of Integer (wrapper class of primitive int), there is no need to use the static method. It is better to just call its toString() method:

Integer number = 2344;
String str = number.toString();
System.out.println(str);    // 2344

String.valueOf() method

The valueOf() method is a static method of the String class that accepts multiple data types like int, long, double, boolean, char, and Object, and returns its string representation:

int number = 2344;
String str = String.valueOf(number);
System.out.println(str);    // 2344

Internally, it calls the toString() method of the corresponding wrapper of the primitive value. For example, in the case of an integer, it calls the Integer.toString() method to perform the conversion. Therefore, it is better to directly use the Integer.toString() method.

StringBuilder & StringBuffer

Both StringBuildeer and StringBuffer are commonly used to concatenate different values into a single String object by using the append() method.

Here is an example that uses the StringBuilder class to convert an integer into a string:

int number = 2344;

StringBuilder builder = new StringBuilder();
builder.append(number);

String str = builder.toString();
System.out.println(str);    // 2344

The StringBuilder class is not thread-safe but is faster, whereas StringBuffer is thread-safe but slower.

String.format() method

The String.format() method returns a formatted string using the specified format string and arguments.

While this method is not meant to convert but rather format a string, it can be used to convert an integer to a string by using the %d format:

int number = 2344;
String str = String.format("%d", number);
System.out.println(str);    // 2344

Apart from conversion, the String.format() method can also be used to format the integer value:

// Text width
String.format("|%10d|", 123);    // |       123|

// Justify left
String.format("|%-10d|", 123);  // |123       |

// Pad with zeros
String.format("|%010d|", 123);  // |0000000123|

// Positive number
String.format("%+d", 123);      // +123

// Thousands separator
String.format("%,d", 1234567);  // 1,234,567

// Enclose -ve number with parenthesis
String.format("%o", 123);    // (123)

To learn more about strings formatting in Java, take a look at this article.

String concatenation

Finally, the last approach to convert an integer to a string is using the string concatenation. Again, this is not the recommended way, as concatenation is not meant for conversion.

When you concatenation an integer value with a string, the result is also a string:

int number = 2344;
String str = "" + number;
System.out.println(str);    // 2344

Conclusion

Converting an integer value into a string is one of the most common operations in Java. In this article, we have covered 5 different ways to achieve this.

The rule of thumb is if the integer variable is a primitive int value, it is better to use the Integer.toString() or String.vaueOf() method.

However, if the variable is already an instance of wrapper class Integer, there is no need to reinvent the wheel. Instead, just call the toString() method of the Integer object to get a string representation of the value.

Read Next: Data Type Conversions in Java to learn about other data type conversions like string to date, a string to float, a string to double, and more.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.