IntegerCache in JDK1.5


What is the O/p of following ?

Integer i1 = 20;
Integer i2 = 20;
Integer i3 = 200;
Integer i4 = 200;

if(i1 == i2){
System.out.println("True");
}else{
System.out.println("False");
}

if(i3 == i4){
System.out.println("True");
}else{
System.out.println("False");
}

if(Integer.valueOf(20) == Integer.valueOf(20)){
System.out.println("True");
}else{
System.out.println("False");
}

if(Integer.valueOf(200) == Integer.valueOf(200)){
System.out.println("True");
}else{
System.out.println("False");
}

The answer is
True
False
True
False
It is because in JDK1.5 there is a new concept called Caching Integer Objects.

Until JDK1.5 it didn’t matter whether to use Integer.valueof() or new Integer() methods to create an integer object.But with the jdk1.5 feature it is recommended to use Integer.valueOf().

Reason : In JDK1.5 the JVM caches Integer objects from range of -128 to 127 . So every time an integer object is create with value between the above mentioned range same object will be returned instead of creating the new object.

For the given statement
Integer i1 = 20.
The autoboxing features come into play which uses again the Integer.valueOf() method to create an object and every time will return same object.

Note: This will not happen for Integer i1 = new Integer(20). // Something similar to String pool

The Integer class has an inner class called IntegerCache with the following implementation.

private static class IntegerCache {
static final int high;
static final Integer cache[];

static {
final int low = -128;

int h = 127;
if (integerCacheHighPropValue != null) {
int i = Long.decode(integerCacheHighPropValue).intValue();
i = Math.max(i, 127);
h = Math.min(i, Integer.MAX_VALUE - -low);
}
high = h;

cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}

private IntegerCache() {}
}

Since its an inner class, so the 256 objects will not be created until it is called for the first time. Hence, initial loading of the first integer object will be slow as cache array with 256 objects will be created.

The valueOf() method implementation is :


public static Integer valueOf(int i) {
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}

Advertisements

6 thoughts on “IntegerCache in JDK1.5

    1. It just occurred to me that if you want to elntiaime the bias (happening when there is more than one line with the minimal random value) by using the iterative process I described above, you can actually do that in one pass over the file.Instead of keeping 1 line and 1 number (the minimum so far) in memory, keep 1 line and an array of numbers in memory. The length of the array is the maximum number of iterations, it can also be an unbounded linked list. Don’t expect many iterations to be necessary, especially if RAND_MAX is big.So, when random() returns the same value as the minimal value so far, call random() twice again. Store the minimum of the two values in the second item of the array. If the first call returns the smallest of the two values, keep the old line. If the second call returns the smallest of the two values, keep the new one. If both are the same, call random() twice again, and store it in the third value of the array. And so on.For later lines, continue to compare the random() return value with the first item in the array first. When it is smaller, keep that line and drop all the further values in the array.

  1. I’m extremely impressed with your writing skills as well as with
    the layout on your blog. Is this a paid theme or did you customize it yourself?
    Either way keep up the excellent quality writing, it is rare to
    see a nice blog like this one today.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s