开发者社区> 问答> 正文

Java Integer 为什么不推荐使用 ==

所有整型包装类对象之间的比较全部使用equals方法比较。

对于 Integer var = ? 在 -128 至 127 范围内的赋值,Integer对象是在IntegerCache.cache产生,会复用已有对象,这个区间内的Integer值可以直接使用 == 进行判断,但是这个区间之外的所有数据,都会在堆上产生,并不会复用已有对象,所以推荐使用equals方法进行判断。

举个栗子:

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

至于为什么会产生这种问题呢,我们可以从源码中找到结果,在Integer.class中有这么一段代码:

/**
     * 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) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

如果 i 的值大于 -128 , 小于 127的时候,就会在IntegerCache.cache中直接获取,如果不是这个区间范围里面的值,就直接 new了,也就是新对象,使用 == 肯定不同了。

我们接着看一下 IntegerCache.cache 是个什么东东。



/**
     * 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 -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) {
                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);
            }
            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() {}
    }
    ```
从注释中,我们可以看出,这是个缓存对象,用来提高访问速度。可以使用-XX:AutoBoxCacheMax=<size> 来控制缓存的大小,

这我们就明白了,如果设置 -XX:AutoBoxCacheMax=200。那上面的两个输出就都是true了。

我就不验证了,有兴趣的小伙伴可以验证一下。但是我还是建议大家,乖乖的使用equals方法来进行判断吧。

展开
收起
游客bnlxddh3fwntw 2020-04-23 20:38:18 4059 0
1 条回答
写回答
取消 提交回答
  • 有点尴尬唉 你要寻找的东西已经被吃掉啦!

    所有整型包装类对象之间的比较全部使用equals方法比较。

    对于 Integer var = ? 在 -128 至 127 范围内的赋值,Integer对象是在IntegerCache.cache产生,会复用已有对象,这个区间内的Integer值可以直接使用 == 进行判断,但是这个区间之外的所有数据,都会在堆上产生,并不会复用已有对象,所以推荐使用equals方法进行判断。

    举个栗子:

    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
    

    至于为什么会产生这种问题呢,我们可以从源码中找到结果,在Integer.class中有这么一段代码:

        /**
         * 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) {
            assert IntegerCache.high >= 127;
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
    

    如果 i 的值大于 -128 , 小于 127的时候,就会在IntegerCache.cache中直接获取,如果不是这个区间范围里面的值,就直接 new了,也就是新对象,使用 == 肯定不同了。

    2020-04-24 23:49:29
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载