Thursday, April 25, 2013

Difference between valueOf and parseInt method in Java

Both valueOf and parseInt methods are used to convert String to Integer in Java, but there are subtle difference between them. If you look at code of valueOf() method, you will find that internally it calls parseInt() method to convert String to Integer, but it also maintains a pool of Integers from -128 to 127 and if requested integer is in pool, it returns object from pool. Which means two integer objects returned using valueOf() method can be same by equality operator. This caching of Immutable object, does help in reducing garbage and help garbage collector. Another difference between parseInt() and valueOf() method is there return type. valueOf() of java.lang.Integer returns an Integer object, while parseInt() method returns an int primitive. Though, after introducing Autoboxing in Java 1.5, this doesn't count as a difference, but it's worth knowing.


ParseInt vs valueOf in Java

Difference between parseInt and valueOf method in JavaIf you look code of parseInt() and valueOf() method from java.lang.Integer class, you will find that actual job of converting String to integer is done by parseInt() method, valueOf() just provide caching of frequently used Integer objects, Here is code snippet from valueOf() method which makes things clear:
public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
}

This method first calls parseInt() method, in order to convert String to primitive int, and then creates Integer object from that value. You can see it internally maintains an Integer cache. If primitive int is within range of cache, it returns Integer object from pool, otherwise it create a new object.
public static Integer valueOf(int i) {
        if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
}

There is always confusion, whether to use parseInt() or valueOf() for converting String to primitive int and java.lang.Integer, I would suggest use parseInt() if you need primitive int and use valueOf() if you need java.lang.Integer objects. Since immutable objects are safe to be pooled and reusing them only reduces load on garbage collector, it's better to use valueOf() if you need Integer object.

5 comments :

Daniel said...

Hi,

I don't understand why pool of Integers from -128 to 127 instead of others number ?

Anonymous said...

All these are basic api's and people usually ignore the internals. But, its too good to know the implementations.
Thank you.

PS: i think there is a typo "parseInt() method to convert Integer to String"- it should be "to convert String to int"

Javin @ Java Classloder Working said...

@Anonymous, You are right, good understanding of basic API e.g. java.lang, java.util are extremely important for effective programming in Java. By the way good catch, parseInt() is used for converting String to primitive int :)

Javin @ Must Override super class method Error said...

@Daniel, I don't know the exact reason, but two observation make sense :

1) Those are most frequently used numbers, so it make sense to cache them.

2) They are also range of bytes -128 to 127

Cheers

SARAL SAXENA said...

@Javin gr8 article , it has clearly explain the concepts ...Actually, valueOf uses parseInt internally. The difference is parseInt returns an int primitive while valueOf returns an Integer object. Consider from the Integer.class source:

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

public static Integer valueOf(String s, int radix) throws NumberFormatException {
return Integer.valueOf(parseInt(s,radix));
}

public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}

As for parsing with a comma, I'm not familiar with one. I would sanitize them.

int million = Integer.parseInt("1,000,000".replace(",",""));

If efficiency is your concern, use int: it is much faster than Integer.

Otherwise, class Integer offers you at least a couple clear, clean ways:

Integer myInteger = new Integer(someString);
Integer anotherInteger = Integer.valueOf(someOtherString);

Post a Comment