阿里巴巴開發手冊(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."<init>":()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=<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的靜態代碼塊,初始化了一個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:

自動裝箱: 就是將基本數據類型自動轉換成對應的包裝類。

自動拆箱:就是將包裝類自動轉換成對應的基本數據類型。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章