Integer.valueOf() vs Integer.parseInt() with Examples
Last Updated :
11 Jul, 2025
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);
}
}
Output: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));
}
}
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);
}
}
- 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. |
Similar Reads
Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples Most of the times, in competitive programming, there is a need to assign the variable, the maximum or minimum value that data type can hold, but remembering such a large and precise number comes out to be a difficult job. Therefore, Java has constants to represent these numbers, so that these can be
3 min read
Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples Most of the times, in competitive programming, there is a need to assign the variable, the maximum or minimum value that data type can hold, but remembering such a large and precise number comes out to be a difficult job. Therefore, Java has constants to represent these numbers, so that these can be
3 min read
Java.lang.Long.valueOf() method with examples Java.lang.Long.valueOf() is a built-in method in Java of lang class that returns a Long object holding the value extracted from a specified String S when parsed with the radix that is given in the second argument. The first argument is interpreted as representing a signed long in the radix specified
2 min read
Float intValue() method in Java with examples The intValue() method in Float Class is a built in method in Java that returns the value specified by the calling object as int after type casting. Syntax: public int intValue() Parameters: The function accepts no parameter. Return Value: It return the value of FloatObject as int. Below programs ill
2 min read
Float intValue() method in Java with examples The intValue() method in Float Class is a built in method in Java that returns the value specified by the calling object as int after type casting. Syntax: public int intValue() Parameters: The function accepts no parameter. Return Value: It return the value of FloatObject as int. Below programs ill
2 min read
ValueRange isIntValue() method in Java with Examples The isIntValue() method of ValueRange class is used to check if all values in the ValueRange fit in an int. This method validates that all valid values are within the bounds of an integer or not. Syntax: public boolean isIntValue() Parameters: This method accepts nothing. Return value: This method r
1 min read