Java Convert Integer to String


There are different ways of converting int to string by using different available methods. In this tutorial, we will learn some of them below.

1. Conversion Using Integer.toString(int)

Integer.toString(int) method takes integer value as an argument and returns the string representation of it.

Basic Syntax

Following is the basic syntax for toString() method which converts int to string and returns the processed string:

public static String toString(int i)

Example

Following is the example for Integer.toString(int i) method:

public class ConvertIntToString 
{ 
  public static void main(String args[]) 
  { 
    // Integer Values
    int firstInteger = 100; 
    int secondInteger = -111;
    int thirdInteger = 500;

    // Converting integers to string
    String firstString = Integer.toString(firstInteger); 
    String secondString = Integer.toString(secondInteger); 
    String thirdString = Integer.toString(thirdInteger); 

    // Printing values after conversion
    System.out.println("String firstString = " + firstString);  
    System.out.println("String secondString = " + secondString);
    System.out.println("Stringg thirdString = " + thirdString); 
  } 
}  

The output is:

String firstString = 100
String secondString = -111
Stringg thirdString = 500

2. Conversion Using String.valueOf(int)

You can also convert int to string using String class. In string class, the static method valueOf takes the integer as an argument and returns the String representation of it.

Basic Syntax

Following is the basic syntax for valueOf() method which converts int to string and returns the processed string:

public static String valueOf(int i)

Example

Following is the example for String.valueOf(int i) method:

public class ConvertIntToString 
{ 
  public static void main(String args[]) 
  { 
    // Integer Values 
    int firstInteger = 230; 
    int secondInteger = -330;
    
    // Convert Integer to String
    String firstString = String.valueOf(firstInteger); 
    String secondString = String.valueOf(secondInteger); 
    
    // Printing values after conversion
    System.out.println("String firstString = " + firstString);  
    System.out.println("String secondString = " + secondString);
  } 
}  

The output should be:

String firstString = 230
String secondString = -330

3. Conversion Using String.format()

You can also use format() method from String class to convert the integer to String.

Basic Syntax

Following is the basic syntax for format() method which converts int to string and returns the processed string:

public static String format(String format, int i)

The above method takes two arguments first is the format and another is value to convert integer i.

Example

Following is the example for String.format(String format,int i) method:

public class ConvertIntToString2 
{ 
  public static void main(String args[]) 
  { 
    // Integer Values 
    int firstInteger = 135; 
    int secondInteger = -170;
    
    // Convert Integer to String
    String firstString = String.format("%d",firstInteger);  
    String secondString = String.format("%d",secondInteger);   
    
    // Printing values after conversion
    System.out.println("String firstString = " + firstString);  
    System.out.println("String secondString = " + secondString);
  } 
}  

In above program %d is used for integer value format like %f is used for Float value format.

The output should be:

String firstString = 135
String secondString = -170

4. Conversion Using StringBuffer/StringBuilder

By using StringBuffer we can concatenate Strings or perform an operation like appending string. by using StringBuffer conversion of int to string takes place in the following way.

Basic Syntax

Following is the basic syntax for format() method which converts int to string and returns the processed string:

public StringBuffer append(int i)

Example

Following is an example for append() method:

public class ConvertIntToString3 
{ 
  public static void main(String args[]) 
  { 
    // Integer Values 
    int firstInteger = 100; 
    int secondInteger = -110;
    
    // Convert Integer to String using StringBuffer
    StringBuffer sb = new StringBuffer(); 
    sb.append(firstInteger); 
    String firstString = sb.toString(); 
    
    // Convert Integer to String using StringBuilder
    StringBuilder sb2 = new StringBuilder(); 
    sb2.append(secondInteger); 
    String secondString = sb2.toString(); 
    
    // Printing values after conversion
    System.out.println("String firstString = " + firstString);  
    System.out.println("String secondString = " + secondString);
  } 
}  

The output should be:

String firstString = 100
String secondString = -110