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.

Convert an integer to a string using Integer.toString()

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 as 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 call its toString() method:

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

Convert an integer to a string using String.valueOf()

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 use the Integer.toString() method.

Convert an integer to a string using StringBuilder & StringBuffer

Both StringBuildeer and StringBuffer are commonly used to concatenate different values into a single String object 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 the StringBuffer is thread-safe but slower.

Convert an integer to a string using String.format()

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 string formatting in Java, read this article.

Convert an integer to a string using string concatenation

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

When you concatenate 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, call the toString() method of the Integer object to get a string representation of the value.

Read this guide to learn about other data type conversions like string to date, a string to float, a string to double, and more in Java.

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