Integer.valueOf() vs Integer.parseInt() with Examples
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 datatype, 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 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); } } |
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 praogram 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)); } } |
Differences between Integer.parseInt() and Integer.valueOf()
- Integer.valueOf() returns an Integer object while Integer.parseInt() returns a primitive int.
// Program to show the use// of Integer.parseInt() methodclassTest1 {publicstaticvoidmain(String args[]){String s ="77";// Primitive int is returnedintstr = Integer.parseInt(str);System.out.print(str);// Integer object is returnedintstr1 = Integer.valueOf(str1);System.out.print(str1);}}chevron_rightfilter_noneCompilation Error in java code :- prog.java:11: error: incompatible types: int cannot be converted to String int str = Integer.parseInt(str); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error - Both String and integer can be passed a parameter to Integer.valueOf() whereas only a String can be passed as parameter to Integer.parseInt().
// Program to show that Integer.parseInt()// cannot take integer as parameterclassTest3 {publicstaticvoidmain(String args[]){intval =99;try{// It can take int as a parameterintstr1 = Integer.valueOf(str1);System.out.print(str1);// It cannot take an int as a parameter// Hence will throw an exceptionintstr = Integer.parseInt(str);System.out.print(str);}catch(Exception e) {System.out.print(e);}}}chevron_rightfilter_noneCompilation Error in java code :- prog.java:18: error: incompatible types: int cannot be converted to String int str = Integer.parseInt(str); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 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.
// Program to test the method// when a character is passed as a parameterclassTest3 {publicstaticvoidmain(String args[]){charval ='A';try{// It can take char as a parameterintstr1 = Integer.valueOf(str1);System.out.print(str1);// It cannot take char as a parameter// Hence will throw an exceptionintstr = Integer.parseInt(str);System.out.print(str);}catch(Exception e) {System.out.print(e);}}}chevron_rightfilter_noneCompilation Error in java code :- prog.java:18: error: incompatible types: int cannot be converted to String int str = Integer.parseInt(str); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 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. |
Recommended Posts:
- scanf("%[^\n]s", str) Vs gets(str) in C with Examples
- JavaFX | Box with examples
- JavaFX | PointLight with examples
- JavaFX | ContextMenu with examples
- JavaFX | CheckMenuItem with examples
- JavaFX | DatePicker with examples
- JavaFX | Alert with examples
- JavaFX | ComboBox with examples
- JavaFX | RadioButton with examples
- JavaFx | ColorPicker with examples
- JavaFX | Cylinder with examples
- JavaFX | QuadCurve with examples
- JavaFX | Polygon with examples
- JavaFX | Circle with examples
- JavaFX | Polyline with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



