日期:2014-05-20  浏览次数:20697 次

设置IntegerCache的上界
用-D可以设置java.lang.Integer.IntegerCache.high,将上限设置大于127,但是当我把它置为小于127(比如20),
然后
Integer a = 10;
Integer b = 10;
System.out.println(a == b);
打出false
不知为何

------解决方案--------------------
小于127没效果。
Java code
static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                int i = parseInt(integerCacheHighPropValue);
                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++);
        }