Java Program to Convert Integer to String

In this Java program, we will learn to convert the integer variables to a string value. Java provides multiple ways to convert an int type variable to a String type.

This program defines an int variable number with the value 25. Then, it demonstrates the use of the Integer.toString() method, the String.valueOf() method, the Integer.toString() method with a radix, the StringBuilder class, and the + operator for converting the int to a String.


Using the Integer.toString() method

int number = 25;
String str = Integer.toString(number);

Integer.toString() method takes an int as an argument and returns the equivalent String representation.


Using the String.valueOf() method

int number = 25;
String str = String.valueOf(number);

String.valueOf() method also takes an int as an argument and returns the equivalent String representation. It can be also used to convert other primitive types like long, float, double etc.


Using the Integer.toString() method with radix

int number = 25;
String str = Integer.toString(number, 16);

Integer.toString() method takes two arguments, the first being the int to convert, and the second being the radix (base) to use for the conversion. In this example, the radix is 16, so the output is the hexadecimal representation of the number.


Using StringBuilder Class

int number = 25;
String str = new StringBuilder().append(number).toString();

This approach uses the append() method to append the int to a StringBuilder object, and then calls the toString() method to get the equivalent String representation.


Using + operator

int number = 25;
String str = "" + number;

This approach uses the + operator to concatenate an empty string and the int, which results in the int being converted to a String.


Java Program to Convert Integer to String

Here is a full Java program that demonstrates the use of the different methods for converting an int to a String.

public class IntToStringExample {
  public static void main(String[] args) {
    int number = 25;

    // Using Integer.toString() method
    String str1 = Integer.toString(number);
    System.out.println("Integer.toString(): " + str1);

    // Using String.valueOf() method
    String str2 = String.valueOf(number);
    System.out.println("String.valueOf(): " + str2);

    // Using Integer.toString() method with radix
    String str3 = Integer.toString(number, 16);
    System.out.println("Integer.toString(radix): " + str3);

    // Using StringBuilder
    String str4 = new StringBuilder().append(number).toString();
    System.out.println("StringBuilder: " + str4);

    // Using '+' operator
    String str5 = "" + number;
    System.out.println("+ operator: " + str5);
  }
}
Output
Integer.toString(): 25
String.valueOf(): 25
Integer.toString(radix): 25
StringBuilder: 25
+ operator: 25

This program defines an int variable number with the value 42. Then, it demonstrates the use of the Integer.toString() method, the String.valueOf() method, the Integer.toString() method with a radix, the StringBuilder class, and the + operator for converting the int to a String.