Integer.parseInt():
While operating upon strings, there are times when we need to convert a number represented as a string into an integer type. The method generally used to convert String to Integer in Java is parseInt(). This method belongs to Integer class in java.lang package. It takes a valid string as a parameter and parses it into primitive data type int. It only accepts String as a parameter and on passing values of any other data type, it produces an error due to incompatible types. There are two variants of this method:Â
Syntax:
public static int parseInt(String s) throws NumberFormatException
public static int parseInt(String s, int radix) throws NumberFormatException
Example:Â
Java
// Java program to demonstrate working parseInt()Â
public class GFG {Â Â Â Â public static void main(String args[])Â Â Â Â {Â Â Â Â Â Â Â Â int decimalExample = Integer.parseInt("20");Â Â Â Â Â Â Â Â int signedPositiveExample = Integer.parseInt("+20");Â Â Â Â Â Â Â Â int signedNegativeExample = Integer.parseInt("-20");Â Â Â Â Â Â Â Â int radixExample = Integer.parseInt("20", 16);Â Â Â Â Â Â Â Â int stringExample = Integer.parseInt("geeks", 29);Â
        System.out.println(decimalExample);        System.out.println(signedPositiveExample);        System.out.println(signedNegativeExample);        System.out.println(radixExample);        System.out.println(stringExample);    }} |
20 20 -20 32 11670324
Integer.valueOf():
This method is a static method belonging to the java.lang package which returns the relevant Integer Object holding the value of the argument passed. This method can take an integer or a String as a parameter. But when the given String is invalid, it provides an error. This method can also take in a character as a parameter but the output will be its corresponding Unicode value. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range. Syntax:
public static Integer valueOf(int a)
public static Integer valueOf(String str)
public static Integer valueOf(String str, int base)
Example:Â
Java
// Java program to illustrate the// java.lang.Integer.valueOf(int a)import java.lang.*;Â
public class Geeks {Â
    public static void main(String[] args)    {Â
        Integer obj = new Integer(10);Â
        // Returns an Integer instance        // representing the specified int value        System.out.println("Output Value = "                           + obj.valueOf(85));    }} |
Output Value = 85
Differences between Integer.parseInt() and Integer.valueOf()
- Integer.valueOf() returns an Integer object while Integer.parseInt() returns a primitive int.
Â
Java
// Program to show the use// of Integer.parseInt() methodÂ
class Test1 {Â Â Â Â public static void main(String args[])Â Â Â Â {Â Â Â Â Â Â Â Â String s = "77";Â
        // Primitive int is returned        int str = Integer.parseInt(s);        System.out.print(str);Â
        // Integer object is returned        int str1 = Integer.valueOf(s);        System.out.print(str1);    }} |
7777
- Both String and integer can be passed a parameter to Integer.valueOf() whereas only a String can be passed as parameter to Integer.parseInt().
Â
Java
// Program to show that Integer.parseInt()// cannot take integer as parameterÂ
class Test3 {Â Â Â Â public static void main(String args[])Â Â Â Â {Â Â Â Â Â Â Â Â int val = 99;Â
        try {Â
            // It can take int as a parameter            int str1 = Integer.valueOf(val);            System.out.print(str1);Â
            // It cannot take an int as a parameter            // Hence will throw an exception            int str = Integer.parseInt(val);            System.out.print(str);        }        catch (Exception e) {            System.out.print(e);        }    }} |
- Compilation Error:
prog.java:18: error: incompatible types:
int cannot be converted to String
int str = Integer.parseInt(val);
^
1 error
- Integer.valueOf() can take a character as parameter and will return its corresponding unicode value whereas Integer.parseInt() will produce an error on passing a character as parameter.
Â
Java
// Program to test the method// when a character is passed as a parameterÂ
class Test3 {Â Â Â Â public static void main(String args[])Â Â Â Â {Â Â Â Â Â Â Â Â char val = 'A';Â
        try {Â
            // It can take char as a parameter            int str1 = Integer.valueOf(val);            System.out.print(str1);Â
            // It cannot take char as a parameter            // Hence will throw an exception            int str = Integer.parseInt(val);            System.out.print(str);        }        catch (Exception e) {            System.out.print(e);        }    }} |
- Compilation Error:
prog.java:18: error: incompatible types:
char cannot be converted to String
int str = Integer.parseInt(val);
^
1 error
Table of difference
| Integer.parseInt() | Integer.valueOf() |
|---|---|
| It can only take a String as a parameter. | It can take a String as well as an integer as parameter. |
| It returns a primitive int value. | It returns an Integer object. |
| When an integer is passed as parameter, it produces an error due to incompatible types | When an integer is passed as parameter, it returns an Integer object corresponding to the given parameter. |
| This method produces an error(incompatible types) when a character is passed as parameter. | This method can take a character as parameter and will return the corresponding unicode. |
| This lags behind in terms of performance since parsing a string takes a lot of time when compared to generating one. | This method is likely to yield significantly better space and time performance by caching frequently requested values. |
| If we need the primitive int datatype then Integer.parseInt() method is to be used. | If Wrapper Integer object is needed then valueOf() method is to be used. |
