xzhuz / blog-gitment

博客备份和comment记录
https://meisen.pro
0 stars 0 forks source link

Java源码之Integer#valueOf #21

Open xzhuz opened 6 years ago

xzhuz commented 6 years ago

https://meisen.pro/article/d2e6e17a7daf4af39212e5a0de0645b2

近日在日常学习中看到一个让我很困惑的知识点:

Integer x = 220;
Integer y = 220;
System.out.println(x == y);

这里控制台输出false. 这就让我十分的不解了,是什么机制导致了这结果?

要搞懂这个,先来讲讲Java的拆装箱。

装箱和拆箱

简单的说,装箱就是,自动将基本数据类型转换为包装器类型;拆箱就是,自动将包装器类型转换为基本数据类型。

基本类型都有对应的包装类型,基本类型与其对应的包装类型之间的赋值使用自动装箱与拆箱完成。

Integer x = 123; // 装箱
int y = x;     // 拆箱

程序在执行到Integer x=123的时候会执行一次自动装箱(int->Integer),这一步的自动装箱实际上是调用了Integer.valueOf(int i)。

这里就出现了一个新的方法: Integer.valueOf(int i),那Integer.valueOf(int i)具体做了什么呢?

自动拆箱的时候是调用: Integer.intValue(), 文章未涉及。

Integer.value(int i)

/**
 * 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);
}

上面是Integer.valueOf(int i)的源码,可以从源码中得出该方法是先判断值是否在缓存池(IntegerCache)的缓存范围,如果在返回缓存池中的值,否则就是新创建一个Integer对象.

我们上面讲到通过字面量给Integer的包装类型引用赋值其实是调用了此方法,代码片段x==y控制台输出false, 我们可以valueOf的源码看出来,xy的赋值其实每次都创建了一个Integer对象,而没有从缓存池中获取值。问题来了,怎么才会从缓存池中获取缓存值呢?

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
                  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() {}
}

上面的代码片段是Java8中,Integer缓存池的源码,我们可以得出Integer缓存池大小默认为-128~127. 所以使用Integer x = 220进行赋值操作每次都会新建一个Integer对象,x == y也就会在控制台输出false了。如果我们像下面的代码片段那样定义,x == y就会输出true。

Integer x = 123;
Integer y = 123;
System.out.println(x ==y); // true

由于有缓存池,编译器会在自动装箱过程调用 valueOf()方法,因此多个Integer实例使用自动装箱来创建并且值相同,那么就会引用相同的对象。

我的理解就是new Integer(123)其实就是一个装箱的过程。

扩展

new Integer(123)和Integer.valueOf(123)的区别

Integer i1 = new Integer(123);
Integer i2 = new Integer(123);
System.out.println(i1 == i2); // false
Integer i3 = Integer.valueOf(123);
Integer i4 = Integer.valueOf(123);
System.out.println(i3 == i4); // true

在Java8 的编译环境中,执行Integer x = 123发现调用了7次Integer.valueOf(i)方法,i的值分别为255,255, 0,30,0,127,0.