溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java中的裝箱拆箱是什么意思

發(fā)布時(shí)間:2021-08-06 18:50:40 來源:億速云 閱讀:125 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要介紹“java中的裝箱拆箱是什么意思”,在日常操作中,相信很多人在java中的裝箱拆箱是什么意思問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java中的裝箱拆箱是什么意思”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

通過本文能了解哪些知識?

  • 為什么基本數(shù)據(jù)類型拆箱有可能會報(bào)空指針異常?

  • 基本數(shù)據(jù)類型的裝箱與拆箱操作是如何實(shí)現(xiàn)的?

 

自動拆箱

首先我們有如下代碼:

public class Test{
    static {
        Integer integer = new Integer(1234);
        int i = integer;
    }
}
 

反編譯其對應(yīng)的class文件,得到如下字節(jié)碼:

public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return
    LineNumberTable:
      line 1: 0

  static {};
    Code:
       0: new           #2                  // class java/lang/Integer
       3: dup
       4: sipush        1234
       7: invokespecial #3                  // Method java/lang/Integer."<init>":(I)V
      10: astore_0
      11: aload_0
      12: invokevirtual #4                  // Method java/lang/Integer.intValue:()I
      15: istore_1
      16: return
    LineNumberTable:
      line 3: 0
      line 4: 11
      line 5: 16
}
 

其中int i = integer對應(yīng)的字節(jié)碼如下:

11: aload_0
12: invokevirtual #4                  // Method java/lang/Integer.intValue:()I
15: istore_1
 

可以看出,對于int i = integer,實(shí)際上等價(jià)于以下語句:

int i = integer.intValue();
 

那么當(dāng)integer等于null的時(shí)候就變?yōu)榱?code>((Integer)null).intValue()了,自然就會拋出空指針異常了;對于其他基本數(shù)據(jù)類型也是同樣的原理

 

自動裝箱

首先需要如下代碼:

public class Test{
    static {
        int i = 1234;
        Integer integer = i;
    }
}
 

反編譯其對應(yīng)的class文件,得到如下字節(jié)碼:

public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return
    LineNumberTable:
      line 1: 0

  static {};
    Code:
       0: sipush        1234
       3: istore_0
       4: iload_0
       5: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       8: astore_1
       9: return
    LineNumberTable:
      line 3: 0
      line 4: 4
      line 5: 9
}
 

其中Integer integer = i對應(yīng)的字節(jié)碼如下:

4: iload_0
5: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
8: astore_1
 

也就是對于Integer integer = i語句,實(shí)際上等價(jià)于:

Integer integer = Integer.valueOf(i);
 

而因?yàn)榛緮?shù)據(jù)類型在java中不存在null值,自然對應(yīng)的自動裝箱操作也就沒有自動裝箱操作的空指針異常風(fēng)險(xiǎn)了。

結(jié)論

最后,我們得到如下結(jié)論,對于基本數(shù)據(jù)類型的自動裝箱操作,實(shí)際上是使用了Integer.valueOf(int)方法,而對于基本數(shù)據(jù)類型對應(yīng)的包裝類型的自動拆箱操作,則是使用了Integer#intValue()方法(注意,此方法是普通方法而不是靜態(tài)方法),所以當(dāng)對應(yīng)的Integer對象為null時(shí)對其進(jìn)行自動拆箱操作就會有空指針風(fēng)險(xiǎn);

到此,關(guān)于“java中的裝箱拆箱是什么意思”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI