Convert string to int in Java

We can convert a string to int in two ways, first using Integer.parseInt() method, and the second is using Integer.valueOf(), the difference between the two approaches is that the second method return an instance of integer class object.
convert string to int
The parseInt() is a static method of the Integer class,here is the example to use it to convert a string to int

the output is

Let’s see the example of the second method Integer.valueOf() to convert string to integer object.

Please note that if we try to convert a string which has no numbers use the Integer.parseInt() or Integer.valueOf() methods,will get the NumberFormatException,for example

The output is

Relate articles

Reverse a string in java