What is the advantage of cache mechanism change of Integer class in JDK 1.6 or above?

integer, java

Solution

Q#1 Why the cache range is using [-128 ~ 127] in jdk 1.5

Because that's the way they designed JDK 1.5.

or default cache of jdk 1.6 or above version?

Because that's the way they designed JDK 1.6.

Is it just to support bytes and char '\u0000'~ 'u007f' ?

In JDK 1.5, yes, in JDK 1.6+, no.

Q#2 What is the advantage to specify a high value for cache range in jdk 1.6 or above version?

So that values in a wider range get cached.

What kind of appilication or sceen is suitable for us to do so?

An application that frequently uses values in a wider range.

Problem

I found cache mechanism is improved in jdk 1.6 or above jdk versions. In jdk 1.5 the cache array in Integer is a fixed one, see ``` static final Integer cache[] = new Integer[-(-128) + 127 + 1]; ``` In jdk 1.6 or above version, an method named `getAndRemoveCacheProperties` and an `IntegerCache.high` property have been added to Integer class, like, // value of java.lang.Integer.IntegerCache.high property (obtained during VM init) ``` private static String integerCacheHighPropValue; static void getAndRemoveCacheProperties() { if (!sun.misc.VM.isBooted()) { Properties props = System.getProperties(); integerCacheHighPropValue = (String)props.remove("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) System.setProperties(props); // remove from system props } } ``` With this change, it is allowed to configure a highest value for cache and to use a new cache range. (`-128 <= cachedValue <= highestValue`). *Here are my questions:* Q#1 Why the cache range is using [-128 ~ 127] in jdk 1.5 or default cache of jdk 1.6 or above version? Is it just to support `bytes` and `char '\u0000'~ 'u007f'` ? Q#2 What is the advantage to specify a high value for cache range in jdk 1.6 or above version? What kind of appilication or sceen is suitable for us to do so? Please help me out with these questions. Thank you very much in advance. Below is the Source code for IntegerCache and valueOf(int i) in Integer class. It just for referance. jdk 1.5 ``` private static class IntegerCache { private IntegerCache(){} static final Integer cache[] = new Integer[-(-128) + 127 + 1]; static { for(int i = 0; i < cache.length; i++) cache[i] = new Integer(i - 128); } } 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); } ``` jdk 1.6 ``` private static class IntegerCache { static final int high; static final Integer cache[]; static { final int low = -128; // high value may be configured by property int h = 127; if (integerCacheHighPropValue != null) { // Use Long.decode here to avoid invoking methods that // require Integer's autoboxing cache to be initialized int i = Long.decode(integerCacheHighPropValue).intValue(); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE 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() {} } public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new Integer(i); } ```

Original source