There are 3 main ways to convert
String to int in Java, using constructor of Integer class, parseInt() method of java.lang.Integer and Integer.valueOf() method.
Though all those method returns instance of java.lang.Integer, which is
a wrapper class for primitive int value, it's easy to convert Integer to int in
Java. From Java 5, you don't need to do anything, autoboxing
will automatically convert Integer to int. For Java 1.4 or lower version, you
can use intValue() method form java.lang.Integer class, to
convert Integer to int. As name suggest, parseInt() is the
core method to convert String to int in Java. parseInt() accept a String
which must contain decimal digits and first character can be an ASCII minus
sign (-) to denote negative integers. parseInt() throws NumberFormatException, if
provided String is not convertible to int value. By the way parseInt is an overloaded
method and it's overloaded version takes radix or base e.g. 2,8,10 or 16,
which can be used to convert binary, octal, hexadecimal String to int in Java. Integer.valueOf() is another
useful method to convert
String to Integer in Java, it offers caching of Integers from -128 to 127.
Internally, valueOf() also calls parseInt() method for String to int conversion. In this Java programming tutorial, we will see all
three ways to convert String to int value in Java.