Convert Int Type Variables to String in Java

Exercise:

Write a Java Program to convert int type variables to string.

Click Here to View the Solution!
public class IntToString {
  public static void main(String[] args) {
    int num1 = 29;
    String str1 = String.valueOf(num1);
    System.out.println(str1);    
  }
} 
Click Here to View the Output!
29
Click Here to View the Explanation!
  • This program deals with turning the values of Integer variables into string equivalent.
  • In the first step, a variable of ‘int’ type, by the name of num1 has been initialized with value 29.
  • For the conversion purpose valueOf() method of String class, is employed.
  • First the variable, num1 is passed to the method, and then the resultant converted values are assigned to String type variables str1.
  • After that the value 29 is  displayed on the console.

%d bloggers like this: