Convert the String to an int in Java
In this tutorial, we are going to learn about how to convert the string to an int in java with the help of examples.
Consider, we have a following string in our Java code:
String price = "21"
Now, we need to convert the above string "21"
to a integer 21
.
Convert the String to int
To convert the string to a int, we can use the built-in Integer.parseInt()
method in java.
The Integer.parseInt()
method takes the string as an argument and converts it to int.
Here is an example:.
String price = "21";
int result = Integer.parseInt(price);
System.out.println(result);
Output:
21