How to Convert Int to String in Java

In this tutorial, we will learn how to convert primitive int or Java wrapper integer class object to string in Java.

Sometimes, we need to convert values from one data type to another, under certain situation. For example, if we need to append an int value to a JTextArea, first we need to convert it to a string before appending to JTextArea.

Therefore, this tutorial will cover seven different ways to convert primitive int or Integer wrapper class to string in Java. Different ways for converting int to string are as:

  • Convert using Integer.toString(int)
  • Convert using String.valueOf(int)
  • Convert using new Integer(int).toString()
  • Convert using String.format()
  • Convert using DecimalFormat
  • Convert using StringBuffer
  • Convert using StringBuffer

Let’s understand all the above ways one by one with example programs.

How to convert int to string in Java

Converting Int to String in Java using String.valueOf()


To convert primitive int to string in Java, we use valueOf() method of the String class. The method String.valueOf() converts int to string. It is a static utility method of the String class.

Therefore, we do not need to create an object to calling this method. We can simply call by using its class name. The general signature of valueOf() method is as below:

public static String valueOf(int i)

This method accepts an integer that needs to be converted and returns the string representation of the int value. Let’s create a simple program to convert a primitive int to string value in Java.

Example 1:

// Java Program to demonstrate the conversion of int into string using String.valueOf().
package javaConversion;
public class IntToStringConversion {
public static void main(String[] args) 
{
   int num = 500;  
// Call valueOf() method of String class for converting int to string. 
   String str = String.valueOf(num);  
   System.out.println(num + 200); // 700 because + is binary plus operator .
   System.out.println(str + 200);// 500200 because + is string concatenation operator.
 }
}
Output:
     700
     500200

Converting Int to String in Java using Integer.toString(int)


The Integer wrapper class in Java also provides a toString() method that converts int to String. The toString() method of Integer class is a static utility method. It returns a string object representing the specified integer value. The general signature of this method is as below:

public static String toString(int i)

Let’s create a simple Java program to convert a primitive int to string in java using Integer.toString() method.


Example 2:

// Java Program to demonstrate the conversion of int into string using Integer.toString().
package javaConversion;
public class IntToStringConversion {
public static void main(String[] args) 
{
   int num = 500;  
// Call toString() method of Integer class for converting int to string. 
   String str = Integer.toString(num);  
   System.out.println(num + 200); // 700 because + is binary plus operator .
   System.out.println(str + 200);// 500200 because + is string concatenation operator.
 }
}
Output:
      700
      500200

Converting Int to String using new Integer(int).toString()


We can also convert int to string by creating Integer class object and then invoking its toString() method. Look at the below example program based on it.

Example 3:

// Java Program to demonstrate the conversion of int into string using new Integer(int).toString().
package javaConversion;
public class IntToStringConversion {
public static void main(String[] args) 
{
  int num = 500;  
  String str = new Integer(num).toString();  
  System.out.println(num + 200); // 700 because + is binary plus operator .
  System.out.println(str + 200);// 500200 because + is string concatenation operator.
 }
}
Output:
      700
      500200

Convert Int to String using String.format()


The String.format() method of String class formats given arguments into String. Java has introduced it since JDK 1.5. This is a new alternative way for converting an Integer class object to a string object.


The purpose of this method is to format a string. The general syntax for this method is as below:

public static String format(String format, Object... args)

Let’s create a Java program to convert a primitive int to string in Java using format() method of String class.

Example 4:

// Java Program to demonstrate the conversion of int into string using format().
package javaConversion;
public class IntToStringConversion {
public static void main(String[] args) 
{
  int num = 200;  
  String str = String.format("%d", num);  
  System.out.println(str); 
 }
}
Output:
      200

Converting int to String using DecimalFormat


DecimalFormat is a concrete subclass of NumberFormat class that formats a number to a string representation. It provides a lot of features designed to parse and format numbers. We can use it to format a number to a string representation using a certain pattern. Here’s an example program using DecimalFormat.

Example 5:

// Java Program to demonstrate the conversion of int into string using DecimalFormat.
package javaConversion;
import java.text.DecimalFormat;
public class IntToStringConversion {
public static void main(String[] args) 
{
  int num = 9898;
  DecimalFormat numberFormat = new DecimalFormat("##,###");
  String str = numberFormat.format(num);        
  System.out.println("Number to be converted is: " + num);
  System.out.println("String representation of 9898 is: " + str);
 }
}
Output:
      Number to be converted is: 9898
      String representation of 9898 is: 9,898

Convert Int to String in Java using StringBuffer


StringBuffer is a class that concatenates multiple values into a single string. It is thread-safe, but it is slower. The StringBuffer object represents a String object that we can change and treat as an array with a sequence of characters.

To add a new value to the end of the string, StringBuffer instance implements the append() method. At the end, call the toString() method, in order to take the string representation of the data.

Here is an example program based on the conversion of int to String in Java.

Example 6:

// Java Program to demonstrate the conversion of int into string using StringBuffer.
package javaConversion;
public class IntToStringConversion {
public static void main(String[] args) 
{
   int num = 98989;
// Create StringBuffer object.  
   StringBuffer sb = new StringBuffer();
   sb.append(num);
   String str = sb.toString();
   System.out.println("String representation: " + str);
 }
}
Output:
      String representation: 98989

You can also write the following code to converting a primitive int type to string in Java.

String str = new StringBuffer().append(98989).toString();
System.out.println("String representation: " + str);

Converting Int to String using StringBuilder


Let’s create a Java program to convert int value to string representation using StringBuilder class. We can change (or modify) the content of StringBuilder, without creating a new StringBuilder object.

Example 7:

// Java Program to demonstrate the conversion of int into string using StringBuilder.
package javaConversion;
public class IntToStringConversion {
public static void main(String[] args) 
{
   int num = 98989;
// Create StringBuilder object.  
   StringBuilder sbBuilder = new StringBuilder();
   sbBuilder.append(num);
   String str = sbBuilder.toString();
   System.out.println("String representation: " + str);
 }
}
Output:
      String representation: 98989

In this tutorial, you learned how to convert a primitive type int to string in Java in several ways. I hope that you will have understood and practiced all programs based on the conversion of int to string in Java.
Thanks for reading!!!