登录

关于java的自动装箱(autoboxing)和拆箱(unboxing)

# Java
PHP中文网PHP中文网2165 天前458 次浏览

全部回复(3) 我要回复

  • 大家讲道理

    大家讲道理2017-04-18 10:46:11

    首先,对于两个 (first == second),因为 first 是 int(基本类型),所以在进行 == 比较的时候,second 会从 Integer 拆箱为 int,然后 == 进行的是值比较 —— 因为两次值(第一次都为 127,第二次都为 128)都相等,所以两次都是 true

    在看两个 (second == third),second 和 third 都是 Integer(对象),所以 == 比较的时候,比较的是对象的引用,如果两个引用指向的是同一块内存(同一个对象),那么返回 true,否则返回 false
    second = 127; 这样的代码其实是 Java 的语法糖,真正执行的是 second = Integer.valueOf(127),我们来看看 Integer.valueOf 的源码:

    很容易理解,Integer 的内部缓存了 IntegerCache.low ~ IntegerCache.high 的 Integer,所以如果传入的整数参数 i 在这个范围之内的话,那么返回的就是缓存的对象,否则才新建一个 Integer。在 Oracle 的 JVM 上,Integer 的缓存范围默认为 -128 ~ 127。所以每次调用 Integer.value(127) 返回的都是同一个被缓存的 Integer,而调用 Integer.value(128) 将每次新建一个 Integer(new Integer(128))。所以第一个(second == third) 返回 true,第二个返回 false。

    回复
    0
  • 怪我咯

    怪我咯2017-04-18 10:46:11

    二个Ineger类型进行==比较,若其值在-128~127返回true,否则返回false,这是由于Integer.valueof()的缓冲对象的原因。

        public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
        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() {}
        }

    回复
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:46:11

        /**
         * 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.
         */
    • 对象和对象比较,会因为一定范围内的缓冲而影响结果。

    • 包装类(对象)和基本类型比较,是直接比值而得出结果。

    回复
    0
  • 取消回复发送