美文网首页Java 杂谈
IntegerCache面试题

IntegerCache面试题

作者: AubreyXue | 来源:发表于2018-08-07 20:48 被阅读21次

    前两天去面试,面试题如下:

    /**
     * IntegerCache
     * @author hrabbit
     */
    public class IntegerCache {
        public static void main(String[] args) {
    
            Integer a=100,b=100,c=200,d=200;
    
            System.out.println(a==b);
            System.out.println(c==d);
        }
    }
    

    输出的结果为:

    true
    false
    

    看到这里有的小伙伴可能比较疑惑了,我们知道==比较的是对象的引用,那这里为什么会这出这种情况呢?

    原理

    首先这是JDK在1.5版本中添加的一项新特性,把-128~127的数字缓存起来了,用于提升性能和节省内存。所以这个范围内的自动装箱(相当于调用valueOf(int i)方法)的数字都会从缓存中获取,返回同一个数字,所以现在你理解为什么了吧。同时这也会给我们开发带来预想不到的陷阱,直得注意!!

    我们来翻看下jdk中Integer的源码

    
        /**
         * Cache to support the object identity semantics of autoboxing for values between
         * -128 and 127 (inclusive) as required by JLS.
         *
         * The cache is initialized on first usage.  The size of the cache
         * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
         * During VM initialization, java.lang.Integer.IntegerCache.high property
         * may be set and saved in the private system properties in the
         * sun.misc.VM class.
         */
    
        private static class IntegerCache {
            static final int low = -128;
            static final int high;
            static final Integer cache[];
    
            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) {
                    try {
                        int i = parseInt(integerCacheHighPropValue);
                        i = Math.max(i, 127);
                        // Maximum array size is Integer.MAX_VALUE
                        h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                    } catch( NumberFormatException nfe) {
                        // If the property cannot be parsed into an int, ignore it.
                    }
                }
                high = h;
    
                cache = new Integer[(high - low) + 1];
                int j = low;
                for(int k = 0; k < cache.length; k++)
                    cache[k] = new Integer(j++);
    
                // range [-128, 127] must be interned (JLS7 5.1.7)
                assert IntegerCache.high >= 127;
            }
    
            private IntegerCache() {}
        }
    

    上面是IntegerCache的源码,把从-128~high放在缓存中

    /**
         * Returns an {@code Integer} instance representing the specified
         * {@code int} value.  If a new {@code Integer} instance is not
         * required, this method should generally be used in preference to
         * the constructor {@link #Integer(int)}, as this method is likely
         * to yield significantly better space and time performance by
         * caching frequently requested values.
         *
         * This method will always cache values in the range -128 to 127,
         * inclusive, and may cache other values outside of this range.
         *
         * @param  i an {@code int} value.
         * @return an {@code Integer} instance representing {@code i}.
         * @since  1.5
         */
        public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
    

    上面是valueOf的源码,先从缓存中获取,获取不到再new一个返回。

    但是为什么JDK要这么多此一举呢? 我们仔细想想, 淘宝的商品大多数都是100以内的价格, 一天后台服务器会new多少个这个的Integer, 用了IntegerCache,就减少了new的时间也就提升了效率。同时JDK还提供cache中high值得可配置,

    这无疑提高了灵活性,方便对JVM进行优化。

    但是面试官的接下来一问,把我问懵了,既然IntegerCache的最大值是一个变量,那么如何修改IntegerCache的最大值呢?

    最后还是面试官告诉我的,通过在VM args中改了一下即可修改

    image.png
    -Djava.lang.Integer.IntegerCache.high=500
    

    这时候测试发现上面的代码输出变成了:

    true
    true
    

    相关文章

      网友评论

        本文标题:IntegerCache面试题

        本文链接:https://www.haomeiwen.com/subject/mqqzvftx.html