In Java, converting a String to an int is done using methods from the Integer class. The methods used for this conversion are Integer.parseInt() and Integer.valueOf().
Example:
The Integer.parseInt() is a commonly used method to convert a string to an integer in Java. This method converts a numeric string to an int by interpreting each character as a digit. If the string contains any non-numeric characters, a NumberFormatException will be thrown.
// Java Program to convert String to int
public class StringToInt {
public static void main(String[] args) {
String s = "123";
// Convert string to int using parseInt
int n = Integer.parseInt(s);
System.out.println("The integer value is: " + n);
}
}
Output
The integer value is: 123