Java Convert Integer to String Example

In this article, we will show you how to convert Java wrapper class Integer or primitive type int to String code examples. Performing conversion from int to String is a common scenario when programming with Core Java.
In this article, we will show you 7 different ways we can do convert Java wrapper class Integer or primitive type int to String.
  1. Convert using Integer.toString(int)
  2. Convert using String.valueOf(int)
  3. Convert using new Integer(int).toString()
  4. Convert using String.format()
  5. Convert using DecimalFormat
  6. Convert using StringBuffer
  7. Convert using StringBuffer

1. Convert using Integer.toString(int)

The Integer wrapper class has a static method that returns a String object representing the specified int parameter. Using this is an efficient solution.
String java.lang.Integer.toString(int i) - toString(int i) method returns a String object representing the specified integer.
int number = 100;
String numberAsString = Integer.toString(number);
System.out.println("Convert using Integer.toString(int) -> " + numberAsString);
Output:
Convert using Integer.toString(int) -> 100

2. Convert using String.valueOf(int)

Returns the string representation of the int argument.
// Convert using String.valueOf(int)
int number = 100;
String numberAsString1 = String.valueOf(number);
System.out.println("Convert using String.valueOf(int) -> " + numberAsString1);
Output:
Convert using String.valueOf(int) -> 100

3. Convert using new Integer(int).toString()

Another alternative method is to create an instance of Integer class and then invoke it's toString() method.
// Convert using new Integer(int).toString()
int number = 100;
String numberAsString2 = new Integer(number).toString();
System.out.println("Convert using new Integer(int).toString() -> " + numberAsString2);
Output:
Convert using new Integer(int).toString() -> 100

4. Convert using String.format()

String.format() is a new alternative that can be used for converting an Integer to a String object.
//Convert using String.format()
int number = 100;
String numberAsString3 = String.format ("%d", number);
System.out.println("Convert using String.format() -> " + numberAsString3);
Output:
Convert using String.format() -> 100

5. Convert using DecimalFormat

The class java.text.DecimalFormat is a class that formats a number to a String representation.
// Convert using DecimalFormat
int number1 = 12345;
DecimalFormat decimalFormat = new DecimalFormat("#");
String numberAsString4 = decimalFormat.format(number1);
System.out.println(numberAsString);
System.out.println("Convert using DecimalFormat -> " + numberAsString4); 
Output:
Convert using DecimalFormat -> 12345

6. Convert using StringBuffer

StringBuffer is a class that is used to concatenate multiple values into a String.
// Convert using StringBuffer Example
int number = 100;
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(number);
String numberAsString5 = stringBuffer.toString();
System.out.println("Convert using StringBuffer Example -> " + numberAsString5);
Output:
Convert using StringBuffer Example -> 100

7. Convert using StringBuilder

// Convert using StringBuilder Example 
int number = 100;
StringBuilder sbBuilder = new StringBuilder();
sbBuilder.append(number);
String numberAsString6 = sbBuilder.toString();
System.out.println("Convert using StringBuilder Example  -> " + numberAsString6);
Output:
Convert using StringBuilder Example  -> 100

Complete Program for Reference

import java.text.DecimalFormat;

/**
 * ConvertIntegerToStringExample
 * @author javaguides.net
 *
 */
public class ConvertIntegerToStringExample {
 public static void main(String[] args) {
  
  // Convert using Integer.toString(int)
  int number = 100;
  String numberAsString = Integer.toString(number);
  System.out.println("Convert using Integer.toString(int) -> " + numberAsString);
  
  // Convert using String.valueOf(int)
  String numberAsString1 = String.valueOf(number);
  System.out.println("Convert using String.valueOf(int) -> " + numberAsString1);
  
  // Convert using new Integer(int).toString()
  String numberAsString2 = new Integer(number).toString();
  System.out.println("Convert using new Integer(int).toString() -> " + numberAsString2);
  
  //Convert using String.format()
  String numberAsString3 = String.format ("%d", number);
  System.out.println("Convert using String.format() -> " + numberAsString3);
  
  // Convert using DecimalFormat
  int number1 = 12345;
  DecimalFormat decimalFormat = new DecimalFormat("#");
  String numberAsString4 = decimalFormat.format(number1);
  System.out.println(numberAsString);
  System.out.println("Convert using DecimalFormat -> " + numberAsString4);
  
  // Convert using StringBuffer Example
  StringBuffer stringBuffer = new StringBuffer();
  stringBuffer.append(number);
  String numberAsString5 = stringBuffer.toString();
  System.out.println("Convert using StringBuffer Example -> " + numberAsString5);
  
  // Convert using StringBuilder Example 
  StringBuilder sbBuilder = new StringBuilder();
  sbBuilder.append(number);
  String numberAsString6 = sbBuilder.toString();
  System.out.println("Convert using StringBuilder Example  -> " + numberAsString6);
  
 }
}
Output:
Convert using Integer.toString(int) -> 100
Convert using String.valueOf(int) -> 100
Convert using new Integer(int).toString() -> 100
Convert using String.format() -> 100
100
Convert using DecimalFormat -> 12345
Convert using StringBuffer Example -> 100
Convert using StringBuilder Example  -> 100

Related Java String Conversion Examples

Comments