Java Program to convert int type variables to String

Example 1: Java Program to Convert int to string using valueOf()

class IntToString {
  public static void main(String[] args) {

    // create two int variable
    int num1 = 56;
    int num2 = 66;

    // convert int to string using valueOf()
    String str1 = String.valueOf(num1);
    String str2 = String.valueOf(num2);

    // print string variables
    System.out.println(str1);    
    System.out.println(str2);    
  }
}
Output
56
66

Example 2: Java Program to Convert int to string using toString()

class IntToString {
  public static void main(String[] args) {

    // create two int variables
    int num1 = 254;
    int num2 = 58676;

    // convert int to string using toString()
    String str1 = Integer.toString(num1);
    String str2 = Integer.toString(num2);

    // print string variables
    System.out.println(str1);    
    System.out.println(str2);    
  }
}

Output

254
58676

Example 3: Java Program to Convert int to String using + Operator

class IntToString {
  public static void main(String[] args) {

    // create two int variables
    int num1 = 5238;
    int num2 = 6878;

    // convert int to string using + sign
    String str1 = "" + num1;
    String str2 = "" + num2;

    // print string variables
    System.out.println(str1);    
    System.out.println(str2);    
  }
}
Output
5238
6878

Example 4: Java Program to Convert int to String using format()

class IntToString {
  public static void main(String[] args) {

    // create a int variable
    int num = 9999;

    // convert int to string using format()
    String str = String.format("%d", num);

    System.out.println(str);    
  }
}
Output
9999

Leave a Reply