Java中的拆箱和裝箱機制

更多知識,請移步我的小破站:http://hellofriend.top

一.裝箱拆箱的定義

衆所周知,Java中有8大基本數據類型,其爲每種基本數據類型都提供了對應的包裝器類型。 下表是基本數據類型對應的包裝器類型:
在這裏插入圖片描述
在Java SE5之前,如果要生成一個數值爲5的Integer對象,必須這樣進行:

Integer i = new Integer(5);

而從Java SE5開始就提供了自動裝箱的特性,如果要生成一個數值爲5的Integer對象,只需要這樣就可以了:

Integer i = 5;

這個過程中會自動根據數值創建對應的 Integer對象,這就是裝箱。

那什麼是拆箱呢?顧名思義,與裝箱對應,拆箱就是自動將包裝器類型轉換爲基本數據類型:

Integer i = 5;  //裝箱
int n = i;   //拆箱

簡單的說,裝箱就是:自動將基本數據類型轉換爲包裝器類型;拆箱就是:自動將包裝器類型轉換爲基本數據類型。

二.裝箱和拆箱的實現

以Interger類爲例,我們看下面一段代碼:

public class Test1 {
    public static void main(String[] args) {
        Integer i = 5;
        int n = i;
    }
}

來看看上面的代碼對應的字節碼文件:
在這裏插入圖片描述
從字節碼內容可以看出,在裝箱的時候自動調用的是Integer的valueOf(int)方法,而在拆箱的時候自動調用的是Integer的intValue方法。

其他的比如Double、Character等也是類似的,大家可以自己動手嘗試一下。
因此可以用一句話總結裝箱和拆箱的實現過程:

裝箱過程是通過調用包裝器的valueOf( )方法實現的,而拆箱過程是通過調用包裝器的 xxxValue方法實現的。(xxx代表對應的基本數據類型)。

三. 典型實例

雖然大多數人對裝箱和拆箱的概念都清楚,但是卻沒有真正掌握其底層原理。下面通過一些常見的裝箱/拆箱有關的問題來加深理解。

1.下面這段代碼的輸出結果是什麼?

public class Main {
    public static void main(String[] args) {
         
        Integer i1 = 100;
        Integer i2 = 100;
        Integer i3 = 200;
        Integer i4 = 200;
         
        System.out.println(i1==i2);
        System.out.println(i3==i4);
    }
}

答案是:
true
false

爲什麼會出現這樣的結果?輸出結果表明i1和i2指向的是同一個對象,而i3和i4指向的是不同的對象。此時只需一看源碼便知究竟,下面這段代碼是Integer的valueOf( )方法的具體實現:

public static Integer valueOf(int i) {
        if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
    }

而其中IntegerCache類的實現爲:

private static class IntegerCache {
        static final int high;
        static final Integer cache[];

        static {
            final int low = -128;

            // high value may be configured by property
            int h = 127;
            if (integerCacheHighPropValue != null) {
                // Use Long.decode here to avoid invoking methods that
                // require Integer's autoboxing cache to be initialized
                int i = Long.decode(integerCacheHighPropValue).intValue();
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - -low);
            }
            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() {}
    }

從上面這2段代碼可以看出,在通過valueOf( )方法創建Integer對象的時候,如果數值在[-128,127]之間,便返回指向IntegerCache.cache中已經存在的對象的引用;否則創建一個新的Integer對象。

上面的代碼中i1和i2的數值爲100,因此會直接從cache中取已經存在的對象,所以i1和i2指向的是同一個對象。而i3和i4則是分別指向不同的對象。

2.下面這段代碼的輸出結果是什麼?

public class Main {
    public static void main(String[] args) {
         
        Double i1 = 100.0;
        Double i2 = 100.0;
        Double i3 = 200.0;
        Double i4 = 200.0;
         
        System.out.println(i1==i2);
        System.out.println(i3==i4);
    }
}

也許有的朋友會認爲跟上面一道題目的輸出結果相同,但是事實上卻不是。實際輸出結果爲:
false
false

這又是爲什麼呢,請看 Double類的valueOf( )具體實現:

public static Double valueOf(double d) {
        return new Double(d);
    }

通過查看Double類的valueOf( )源碼, 我們發現其直接返回了一個新的 Double 對象,而非像Integer類型一樣先去判斷數值大小。 原因其實很簡單:在某個範圍內的整型數值的個數是有限的,而浮點數卻不是。

總結,Integer、Short、Byte、Character、Long這幾個類的valueOf( )方法的實現是類似的。
Double、Float的valueOf( )方法的實現是類似的。

3.下面這段代碼輸出結果是什麼:

public class Main {
    public static void main(String[] args) {
         
        Boolean i1 = false;
        Boolean i2 = false;
        Boolean i3 = true;
        Boolean i4 = true;
         
        System.out.println(i1==i2);
        System.out.println(i3==i4);
    }
}

輸出結果是:
true
true

至於爲什麼是這個結果,同樣地,看了Boolean類的源碼也會一目瞭然。下面是Boolean的valueOf( )方法的具體實現:

public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
}

而其中的 TRUE 和FALSE又是什麼呢?在Boolean中定義了2個靜態成員屬性:

public static final Boolean TRUE = new Boolean(true);

public static final Boolean FALSE = new Boolean(false);

至此,大家應該明白了爲何上面輸出的結果都是true了。

結語

相信經過全文的敘述,大家應該能對Java中的裝箱和拆箱機制有一個較深入的理解,如果您發現了文章的錯誤歡迎留言糾正。

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