Exercise:
Write a Java Program to convert string type variables into int.
Click Here to View the Solution!
public class IntToString {
public static void main(String[] args) {
int num1 =
98;
String str1 = String.valueOf(num1);
System.out.println(str1);
}
}
Click Here to View the Output!
98
Click Here to View the Explanation!
- This program carries out the task of transforming a given string value into its Integer equivalent.
- For the conversion purpose
valueOfmethod
ofInteger
class, is employed. - First a
String
type variables str1 is declared. - Then the variable is passed to
valueOf()
method as argument. - The very next step involves the assignment of the resulting value to int type variable
num1
. - Finally, the result is displayed.
- This method
valueOf()
differs fromparseInt()
in a sense that it first converts the values into an integer object then those objects are turned into integer values. - This process is termed as ‘unboxing’, specifically in Java.