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. |