VOOZH about

URL: https://www.geeksforgeeks.org/java/integer-valueof-vs-integer-parseint-with-examples/

⇱ Integer.valueOf() vs Integer.parseInt() with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Integer.valueOf() vs Integer.parseInt() with Examples

Last Updated : 27 May, 2026

When working with Strings in Java, we often need to convert numeric text into integer values. The Integer class provides two commonly used methods for this purpose: parseInt() and valueOf().

  • Integer.parseInt() returns a primitive int.
  • Integer.valueOf() returns an Integer wrapper object.
  • Both methods can convert numeric Strings, but they differ in return type and usage.

Integer.parseInt()

The parseInt() method is used to convert a numeric String into a primitive int. It belongs to the Java Integer class in the java.lang package.

  • Accepts only String as parameter.
  • Returns primitive int.
  • Throws NumberFormatException for invalid Strings.
  • Supports different number systems using radix.

Syntax

Use this when the number is in normal decimal format (base 10).

public static int parseInt(String s) throws NumberFormatException

Use this when the number belongs to a different number system like Binary (base 2),Octal (base 8), etc The radix tells Java which base to use.

public static int parseInt(String s, int radix) throws NumberFormatException


Output
20
20
-20
32
11670324

Explanation: This program shows how Integer.parseInt() converts different String values into primitive int, either in default decimal form or using a specified radix like 16 or 29 to interpret the number in different number systems before converting it into decimal output.

Integer.valueOf()

The Integer.valueOf() method converts a value into an Integer object. It is also a static method of the Integer class in the java.lang package.

  • Returns an Integer object.
  • Accepts both String and int.
  • Supports radix conversion.
  • Uses caching for values between -128 to 127, improving performance and memory usage.

Syntax

Converts a primitive int value into its corresponding Integer wrapper object.

public static Integer valueOf(int a)

The java.lang.Integer.valueOf(String str) is an inbuilt method which is used to return an Integer object, holding the value of the specified String str.

public static Integer valueOf(String str)

The java.lang.Integer.valueOf(String s, int radix) is an inbuilt method which returns an Integer object, holding the value extracted from the specified String when parsed with the base given by the second argument

public static Integer valueOf(String s, int radix)


Output
Output Value = 85

Explanation: This program demonstrates how Integer.valueOf() converts primitive int values into Integer wrapper objects and returns them, which are then printed as their corresponding integer values in the output.

Integer.parseInt() vs Integer.valueOf()

FeatureInteger.parseInt()Integer.valueOf()
Return Typeint (primitive type)Integer (wrapper object)
PurposeConverts String to primitive intConverts String/int to Integer object
SyntaxInteger.parseInt("123")Integer.valueOf("123")
Input TypesOnly StringString or int
Object CreationNo object creationMay create or reuse Integer object
Memory UsageLess memorySlightly more memory
PerformanceFasterSlightly slower
Use CaseWhen you need primitive int for calculationsWhen you need Integer object (collections, generics)
CachingNot applicableCaches values from -128 to 127 (Integer cache)
Null HandlingThrows NumberFormatExceptionThrows NumberFormatException
Radix SupportYes (parseInt(String, radix))Yes (valueOf(String, radix))

Some Common Differences

1. Return Type Difference

Integer.valueOf() returns an Integer object while Integer.parseInt() returns a primitive int.


Output
Using valueOf(): 100 Type: Integer (Object)
Using parseInt(): 100 Type: int (Primitive)

Explanation: This program shows the difference between Integer.valueOf() and Integer.parseInt() where valueOf() converts a String into an Integer object, while parseInt() converts the same String into a primitive int, and both produce the same numeric output but differ in return type and usage.

2. Parameter Type Difference

Integer.valueOf() accepts both String and int (and char via implicit conversion) as input, whereas Integer.parseInt() accepts only a String as input.

Output :

Compilation Error

error: incompatible types: int cannot be converted to String
int num = Integer.parseInt(val);

^
1 error

Explanation: This program shows that Integer.valueOf() can directly convert an int into an Integer object, while Integer.parseInt() does not accept an int as input and only works with String values, highlighting the difference in their accepted parameter types.

3. Character Input Difference

Integer.valueOf() can take a character as input (via automatic conversion to its Unicode/ASCII value), whereas Integer.parseInt() does not accept a character and gives a compile-time error.

Output:

Compilation Error

error: incompatible types: char cannot be converted to String
int num = Integer.parseInt(ch);

^
1 error

Explanation: This program shows that Integer.valueOf() can convert a char into an Integer object by using its Unicode (ASCII) value, while Integer.parseInt() does not accept a char as input and only works with String values, which leads to a compile-time error in this case.

4. Performance Comparison

parseInt() is generally faster than valueOf() because it returns a primitive int and avoids object creation overhead.

Output values depend on system, but pattern is always same


Output
parseInt time : 72735920
valueOf time : 80890529

Explanation:

  • parseInt() converts String → primitive int directly, so no extra object is created.
  • valueOf() converts String → Integer object, so JVM may create or reuse objects (autoboxing + caching), which adds extra time.
Comment