Difference between Integer.parseInt() vs Integer.valueOf() in Java

Both the Integer.parseInt() and Integer.valueOf() method is used to convert a String into Integer in Java, both does the same work then why there is a need for two different method will be the question here. They both does the same job but has a slight difference among them. Lets look into the difference between parseInt vs valueOf in Java

Difference between parseInt vs valueOf in Java

The main difference between Integer.parseInt() vs Integer.valueOf() would be

  • parseInt() :  will be returning the primitive type int
  • valueOf() : will be returning the Integer wrapper Object

After the introduction of Autoboxing and Unboxing in Java in 1.5, it will not make much of a difference but worth knowing.

The parseXXX() method and valueOf() is almost present in most of the numeric primitive datatypes wrapper class, such as Integer, Long, Double, Float, etc.

When we look into the source code of parseInt() and valueOf() in java.lang.Integer class

public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);

parseInt() method simply parses the String which is passed to it and returns primitive int.


public static Integer valueOf(String s, int radix) throws NumberFormatException {
    return Integer.valueOf(parseInt(s,radix));
public static Integer valueOf(int i) {
	final int offset = 128;
	if (i >= -128 && i <= 127) { // must cache
		return IntegerCache.cache[i + offset];
	return new Integer(i);

As we can see from the above code, valueOf() method passes the String to the parseInt() method which performs the actual conversion of String and returns the Wrapper Integer. Then it calls the actual valueOf() method, which maintains the pool of Integers ranging -128 to 127 and if the primitive int is within the cache range it returns the Integer from object pool and if the primitive is not within the cache range it will create a new object.

There is always a confusion whether to use parseInt() or valueOf() method, the best option would be if we need the primitive int datatype then we can go for parseInt() method. If you want Wrapper Integer object then go for valueOf() method.

Leave a Reply

Your email address will not be published. Required fields are marked *