阿里巴巴开发手册(2) - 包装类对象之间值的比较

前言

之前对于阿里巴巴开发手册的一些内容只是有所了解,知道不能那么去做,却没有进行过一些自己的研究。这篇文章结合自己平常编码时确实遇到过的问题,与阿里巴巴开发手册上的点进行分析。

问题点:关于包装类对象之间值的比较

7.【强制】所有的相同类型的包装类对象之间值的比较,全部使用 equals 方法比较。

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

通过手册的描述,我们可以大致了解到,比较两个Integer对象的值时,有以下两种比较方式及注意事项:

方式 注意事项
使用 == 进行比较 比较范围:-128 ~ 127
使用equals方法进行比较 暂无

以Integer为例,验证demo:

public class IntegerTest {

    public static void main(String[] args) {
        Integer a = 128;
        Integer aa = 128;
        Integer b = 127;
        Integer bb = 127;

        System.out.println(a == aa);
        System.out.println(a.equals(aa));
        System.out.println(b == bb);
    }
}

输出结果:

false
true
true

分析

Integer包装类具有自动拆装箱机制,我们为了查看其具体的调用方法。将上面的代码反编译以后,得到结果如下:

"D:\Program Files\Java\jdk1.8.0_101\bin\javap.exe" -c alibaba.IntegerTest
Compiled from "IntegerTest.java"
public class alibaba.IntegerTest {
  public alibaba.IntegerTest();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: sipush        128
       3: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       6: astore_1
       7: sipush        128
      10: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      13: astore_2
      14: bipush        127
      16: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      19: astore_3
      20: bipush        127
      22: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      25: astore        4
      27: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
      30: aload_1
      31: aload_2
      32: if_acmpne     39
      35: iconst_1
      36: goto          40
      39: iconst_0
      40: invokevirtual #4                  // Method java/io/PrintStream.println:(Z)V
      43: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
      46: aload_1
      47: aload_2
      48: invokevirtual #5                  // Method java/lang/Integer.equals:(Ljava/lang/Object;)Z
      51: invokevirtual #4                  // Method java/io/PrintStream.println:(Z)V
      54: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
      57: aload_3
      58: aload         4
      60: if_acmpne     67
      63: iconst_1
      64: goto          68
      67: iconst_0
      68: invokevirtual #4                  // Method java/io/PrintStream.println:(Z)V
      71: return
}

Process finished with exit code 0

可以看出,Integer a = 128 这行代码实际上在编译后转换成了 Integer a = Integer.valueOf(128)


接下来看下,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);
    }

在这个方法里,先判断值是否处于IntegerCache缓存的范围内,如果在范围内的话,则从缓存中获取,如果不在的话,则实例化一个新的Integer对象


IntegerCache 是一个Integer的静态内部类,项目启动时初始化好,来看下IntegerCache的实现:

 /**
     * 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=} 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的静态代码块,初始化了一个Integer数组cache[],cache[]里的值具有范围,默认值为 -128 ~ 127,这个范围可以通过修改jvm参数 -XX:AutoBoxCacheMax=,进行调整

总结

通过以上分析,在没有修改过jvm参数时

当Integer a = 值在-128 ~ 127之间时,实际上取到的是在IntegerCache缓存里的对象,而== 用于对象比较时,比较的是对象的引用地址。属于同一个对象引用,所以进行==比较时,返回true

当Integer a = 值不在-128 ~ 127之间时,会new一个新的对象,使用==比较时,就会返回false

equlse为什么可以返回正确结果呢?

原因在于在Integer里equlse方法是这么实现的:

public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

比较的是Integer实例里的保存基本类型值int,所以是正确的

tips:

自动装箱: 就是将基本数据类型自动转换成对应的包装类。

自动拆箱:就是将包装类自动转换成对应的基本数据类型。

你可能感兴趣的:(阿里巴巴开发手册)