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

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()

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.

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

Java `

public class GFG { public static void main(String args[]) {

    int decimalExample = Integer.parseInt("20");
    int signedPositiveExample = Integer.parseInt("+20");
    int signedNegativeExample = Integer.parseInt("-20");

    // "20" in base 16 = 32 in decimal
    int radixExample = Integer.parseInt("20", 16);

    // "geeks" in base 29
    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

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

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)

Java `

public class Geeks {

public static void main(String[] args) {

    Integer obj = Integer.valueOf(10);

    System.out.println("Output Value = "
                       + Integer.valueOf(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()

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

Some Common Differences

1. Return Type Difference

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

Java `

public class Geeks { public static void main(String[] args) {

    // Integer object
    Integer obj = Integer.valueOf("100");

    // primitive int
    int num = Integer.parseInt("100");

    System.out.println("Using valueOf(): " + obj + "  Type: Integer (Object)");
    System.out.println("Using parseInt(): " + num + "  Type: int (Primitive)");
}

}

`

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.

Java `

public class Test3 { public static void main(String args[]) {

    int val = 99;

    // valueOf works (int → Integer)
    Integer obj = Integer.valueOf(val);
    System.out.println("valueOf output: " + obj);

    // parseInt works ONLY with String
    int num = Integer.parseInt(val);
    System.out.println("parseInt output: " + num);


}

}

`

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

Java `

class Geeks { public static void main(String[] args) {

    char ch = 'A';

    // valueOf() works (char → int → Integer object)
    Integer obj = Integer.valueOf(ch);
    System.out.println("valueOf output: " + obj);

    // ParseInt() will cause compile-time error
    int num = Integer.parseInt(ch);
    System.out.println("parseInt output: " + num);
}

}

`

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

Java `

public class Geeks { public static void main(String[] args) {

    String s = "100000";

    long start1 = System.nanoTime();
    for (int i = 0; i < 1000000; i++) {
        int a = Integer.parseInt(s);
    }
    long end1 = System.nanoTime();

    long start2 = System.nanoTime();
    for (int i = 0; i < 1000000; i++) {
        Integer b = Integer.valueOf(s);
    }
    long end2 = System.nanoTime();

    System.out.println("parseInt time  : " + (end1 - start1));
    System.out.println("valueOf time   : " + (end2 - start2));
}

}

`

Output values depend on system, but pattern is always same

Output

parseInt time : 72735920 valueOf time : 80890529

**Explanation: