欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳細(xì)說(shuō)一說(shuō)Java自動(dòng)裝箱與拆箱是什么

 更新時(shí)間:2025年10月19日 09:27:31   作者:程序員小假  
自動(dòng)裝箱與拆箱的機(jī)制可以讓我們?cè)贘ava的變量賦值或者是方法調(diào)用等情況下使用原始類型或者對(duì)象類型更加簡(jiǎn)單直接,這篇文章主要介紹了Java自動(dòng)裝箱與拆箱是什么的相關(guān)資料,需要的朋友可以參考下

一、核心概念:什么是裝箱與拆箱?

要理解“自動(dòng)”,首先要理解手動(dòng)的“裝箱”和“拆箱”。

Java 是一個(gè)面向?qū)ο蟮恼Z(yǔ)言,但為了效率,它同時(shí)包含了兩種不同的類型系統(tǒng):

  1. 基本數(shù)據(jù)類型byteshortintlongfloatdoublecharboolean。它們直接存儲(chǔ)“值”,存在于棧內(nèi)存中,效率高。

  2. 引用類型:所有 Object 的子類。它們存儲(chǔ)的是對(duì)象的“引用”(地址),實(shí)際對(duì)象存在于堆內(nèi)存中。

在某些場(chǎng)景下(例如使用集合類 ArrayListHashMap),我們必須使用引用類型,因?yàn)榧现荒艽鎯?chǔ)對(duì)象。這就產(chǎn)生了在基本類型和其對(duì)應(yīng)的包裝類對(duì)象之間轉(zhuǎn)換的需求。

包裝類:Java 為每一個(gè)基本類型都提供了一個(gè)對(duì)應(yīng)的“包裝類”,將這些基本類型“包裝”成對(duì)象。

基本數(shù)據(jù)類型包裝類
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

1. 手動(dòng)裝箱

將一個(gè)基本數(shù)據(jù)類型的值,包裝成對(duì)應(yīng)的包裝類對(duì)象。

// 手動(dòng)裝箱
int i = 10;
Integer integerObj = Integer.valueOf(i); // 方式一:推薦,使用了緩存(后面會(huì)講)
// 或者
Integer integerObj2 = new Integer(i);    // 方式二:已過(guò)時(shí) (Deprecated),不推薦

2. 手動(dòng)拆箱

從一個(gè)包裝類對(duì)象中,提取出它所包裹的基本數(shù)據(jù)類型的值。

// 手動(dòng)拆箱
Integer integerObj = new Integer(10);
int j = integerObj.intValue(); // 從 Integer 對(duì)象中取出 int 值

二、什么是自動(dòng)裝箱與拆箱?

從 Java 5 開(kāi)始,為了簡(jiǎn)化開(kāi)發(fā),編譯器提供了自動(dòng)裝箱自動(dòng)拆箱的功能。它本質(zhì)上是一種“語(yǔ)法糖”,編譯器在背后自動(dòng)幫我們插入了轉(zhuǎn)換代碼,讓我們可以用更簡(jiǎn)潔的方式編寫(xiě)。

1. 自動(dòng)裝箱

編譯器自動(dòng)將基本數(shù)據(jù)類型轉(zhuǎn)換為對(duì)應(yīng)的包裝類對(duì)象。

// 自動(dòng)裝箱
int i = 10;
Integer integerObj = i; // 編譯器背后實(shí)際執(zhí)行的是:Integer integerObj = Integer.valueOf(i);

在這行代碼中,一個(gè) int 類型的變量 i 被直接賦給了一個(gè) Integer 類型的引用。編譯器在編譯時(shí),會(huì)悄悄地調(diào)用 Integer.valueOf(i) 來(lái)完成轉(zhuǎn)換。

2. 自動(dòng)拆箱

編譯器自動(dòng)將包裝類對(duì)象轉(zhuǎn)換為對(duì)應(yīng)的基本數(shù)據(jù)類型。

// 自動(dòng)拆箱
Integer integerObj = new Integer(10);
int j = integerObj; // 編譯器背后實(shí)際執(zhí)行的是:int j = integerObj.intValue();

在這里,一個(gè) Integer 對(duì)象被直接賦給了一個(gè) int 類型的變量。編譯器在編譯時(shí),會(huì)悄悄地調(diào)用 integerObj.intValue() 來(lái)完成轉(zhuǎn)換。

三、實(shí)際應(yīng)用場(chǎng)景舉例

自動(dòng)裝箱和拆箱讓我們的代碼變得非常簡(jiǎn)潔,尤其是在使用集合類時(shí)。

// 在 Java 5 之前,使用 ArrayList 非常麻煩
ArrayList list = new ArrayList();
list.add(Integer.valueOf(1)); // 手動(dòng)裝箱
int value = (Integer) list.get(0)).intValue(); // 取出來(lái)是 Object,要強(qiáng)轉(zhuǎn),再手動(dòng)拆箱

// 在 Java 5 之后,有了泛型和自動(dòng)裝箱/拆箱
ArrayList<Integer> list = new ArrayList<>();
list.add(1); // 自動(dòng)裝箱:int -> Integer
int value = list.get(0); // 自動(dòng)拆箱:Integer -> int。代碼清晰多了!

其他常見(jiàn)場(chǎng)景:

// 1. 方法調(diào)用時(shí)傳遞參數(shù)
public void processInteger(Integer i) { ... }
processInteger(5); // 自動(dòng)裝箱,將 int 5 轉(zhuǎn)為 Integer

// 2. 運(yùn)算符運(yùn)算時(shí)
Integer a = 10;
Integer b = 20;
int c = a + b; // a 和 b 先自動(dòng)拆箱為 int,相加后結(jié)果再自動(dòng)裝箱賦給 Integer(如果接收類型是Integer)
// 等價(jià)于:int c = a.intValue() + b.intValue();

// 3. 三目運(yùn)算符中
boolean flag = true;
Integer i = flag ? 100 : 200; // 100和200都會(huì)被自動(dòng)裝箱為Integer

四、注意事項(xiàng)與陷阱(非常重要?。?/h2>

自動(dòng)裝箱雖然方便,但也引入了一些容易忽略的陷阱。

1. 空指針異常

因?yàn)樽詣?dòng)拆箱實(shí)際上是調(diào)用了 xxxValue() 方法,如果包裝類對(duì)象是 null,調(diào)用方法就會(huì)拋出 NullPointerException。

Integer nullInteger = null;
int i = nullInteger; // 運(yùn)行時(shí)拋出 NullPointerException!
// 背后是:int i = nullInteger.intValue();

2. 性能消耗

雖然單次的裝箱/拆箱開(kāi)銷很小,但在循環(huán)次數(shù)極多(例如上億次)的場(chǎng)景下,頻繁的創(chuàng)建和銷毀對(duì)象會(huì)帶來(lái)不必要的性能開(kāi)銷。

long start = System.currentTimeMillis();
Long sum = 0L; // 這里用的是包裝類 Long,會(huì)觸發(fā)自動(dòng)裝箱
for (long i = 0; i < Integer.MAX_VALUE; i++) {
    sum += i; // 每次循環(huán):i自動(dòng)裝箱為L(zhǎng)ong,然后sum拆箱為long,相加后再裝箱為L(zhǎng)ong
}
long end = System.currentTimeMillis();
System.out.println("耗時(shí):" + (end - start));
// 將 Long sum = 0L 改為 long sum = 0L,性能會(huì)有巨大提升。

3. 相等比較的陷阱

第一題:以下的代碼會(huì)輸出什么?

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

運(yùn)行結(jié)果:

true
false

為什么會(huì)出現(xiàn)這樣的結(jié)果?輸出結(jié)果表明 i1 和 i2 指向的是同一個(gè)對(duì)象,而 i3 和 i4 指向的是不同的對(duì)象。此時(shí)只需一看源碼便知究竟,下面這段代碼是 Integer 的 valueOf 方法的具體實(shí)現(xiàn):

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

其中 IntegerCache 類的實(shí)現(xiàn)為:

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

在通過(guò) valueOf 方法創(chuàng)建 Integer 對(duì)象的時(shí)候,如果數(shù)值在 [-128,127] 之間,便返回指向 IntegerCache.cache 中已經(jīng)存在的對(duì)象的引用;否則創(chuàng)建一個(gè)新的 Integer 對(duì)象。

上面的代碼中 i1 和 i2 的數(shù)值為100,因此會(huì)直接從 cache 中取已經(jīng)存在的對(duì)象,所以 i1 和 i2 指向的是同一個(gè)對(duì)象,而 i3 和 i4 則是分別指向不同的對(duì)象。

第二題:以下的代碼會(huì)輸出什么?

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

運(yùn)行結(jié)果:

false
false

原因: 在某個(gè)范圍內(nèi)的整型數(shù)值的個(gè)數(shù)是有限的,而浮點(diǎn)數(shù)不是

4. 三目運(yùn)算符的陷阱

這是一個(gè)非常隱蔽的陷阱。

boolean flag = true;
Integer i = flag ? 100 : Integer.valueOf(200);
// 這沒(méi)問(wèn)題,因?yàn)?100 和 Integer.valueOf(200) 類型“一致”(都是Integer對(duì)象)

Integer i = flag ? 100 : null;
// 當(dāng) flag 為 false 時(shí),會(huì)發(fā)生什么?
// 編譯器認(rèn)為 100 是 int,null 是 Integer。為了類型一致,它會(huì)將 100 自動(dòng)裝箱為 Integer,將 null 作為 Integer。
// 所以這里不會(huì)報(bào)錯(cuò),i 會(huì)被賦值為 null。

int j = flag ? 100 : Integer.valueOf(200);
// 這也沒(méi)問(wèn)題,因?yàn)?Integer.valueOf(200) 會(huì)被自動(dòng)拆箱為 int。

int k = flag ? 100 : null;
// 當(dāng) flag 為 false 時(shí),會(huì)發(fā)生 NullPointerException!
// 因?yàn)榫幾g器需要得到一個(gè) int 類型的結(jié)果,所以它會(huì)嘗試對(duì) `null` 進(jìn)行自動(dòng)拆箱,調(diào)用 null.intValue()。

總結(jié)

  • 自動(dòng)裝箱基本類型 -> 包裝類,編譯器調(diào)用 valueOf()。

  • 自動(dòng)拆箱包裝類 -> 基本類型,編譯器調(diào)用 xxxValue()

  • 優(yōu)點(diǎn):簡(jiǎn)化代碼,使基本類型和包裝類之間的轉(zhuǎn)換無(wú)縫進(jìn)行。

  • 陷阱

    1. 空指針:包裝類為 null 時(shí)拆箱會(huì)拋 NPE。

    2. 性能:在密集循環(huán)中可能帶來(lái)開(kāi)銷。

    3. 比較== 比較包裝類是比較地址,應(yīng)使用 equals 或先拆箱。

    4. 三目運(yùn)算符:要注意類型的統(tǒng)一,避免意外的自動(dòng)拆箱。

到此這篇關(guān)于Java自動(dòng)裝箱與拆箱是什么的文章就介紹到這了,更多相關(guān)Java自動(dòng)裝箱與拆箱內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論