![]() |
VOOZH | about |
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().
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.
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
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.
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.
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 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.
| Feature | Integer.parseInt() | Integer.valueOf() |
|---|---|---|
| Return Type | int (primitive type) | Integer (wrapper object) |
| Purpose | Converts String to primitive int | Converts String/int to Integer object |
| Syntax | Integer.parseInt("123") | Integer.valueOf("123") |
| Input Types | Only String | String or int |
| Object Creation | No object creation | May create or reuse Integer object |
| Memory Usage | Less memory | Slightly more memory |
| Performance | Faster | Slightly slower |
| Use Case | When you need primitive int for calculations | When you need Integer object (collections, generics) |
| Caching | Not applicable | Caches values from -128 to 127 (Integer cache) |
| Null Handling | Throws NumberFormatException | Throws NumberFormatException |
| Radix Support | Yes (parseInt(String, radix)) | Yes (valueOf(String, radix)) |
Integer.valueOf() returns an Integer object while Integer.parseInt() returns a primitive int.
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.
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.
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.
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
parseInt time : 72735920 valueOf time : 80890529
Explanation: