自動裝箱和自動拆箱源碼分析_第1頁
自動裝箱和自動拆箱源碼分析_第2頁
自動裝箱和自動拆箱源碼分析_第3頁
自動裝箱和自動拆箱源碼分析_第4頁
自動裝箱和自動拆箱源碼分析_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、自動裝箱(boxing)和自動拆箱(unboxing)首先了解下Java的四類八種基本數(shù)據(jù)類型基本類型占用空間(Byte)表示范圍包裝器類型boolean1/8true|falseBooleanchar2-128127Characterbyte1-128127Byteshort2-215215-1Shortint4-231231-1Integerlong8-263263-1Longfloat4-3.403E383.403E38Floatdouble8-1.798E3081.798E308Double自動裝箱Java中所謂的裝箱通俗點就是:八種基本數(shù)據(jù)類型在某些條件下使用時,會自動變?yōu)閷?yīng)的包裝

2、器類型。如下清單1:Testpublic void boxingTest() Integer i1 = 17;Integer i2 = 17;Integer i3 = 137;Integer i4 = 137;System.out.println(i1 = i2);11 System.out.println(i3 = i4);輸出:truefalse解釋下清單1第11句輸出true的原因:當(dāng)包裝器類型進行“=”比較時,i3會調(diào)用Integer.valueOf自動裝箱基本數(shù)據(jù)類型為包裝器類型。/* Returns an code Integer instance representing the

3、 specified* code int value. If a new code Integer instance is not* required, this method should generally be used in preference to* the constructor link #Integer(int), as this method is likely* to yield significantly better space and time performance by* caching frequently requested values.* This me

4、thod will always cache values in the range -128 to 127,* inclusive, and may cache other values outside of this range.* param i an code int value.* return an code Integer instance representing code i.* since 1.5*/public static Integer valueOf(int i) if (i >= IntegerCache.low && i <= Int

5、egerCache.high)return IntegerCache.cachei + (-IntegerCache.low);return new Integer(i);從源碼中可以看出,Integer對象自動緩存int值范圍在lowhigh(-128127),如果超出這個范圍則會自動裝箱為包裝類。Note:1. Integer、Short、Byte、Character、Long這幾個包裝類的valueOf方法的實現(xiàn)是類似的;2. Double、Float的valueOf方法的實現(xiàn)是類似的。3. Boolean的valueOf方法的實現(xiàn)是個三目運算,形如  return&

6、#160;(b ? TRUE : FALSE);  自動拆箱Java中所謂的拆箱通俗點就是:八種包裝器類型在某些條件下使用時,會自動變?yōu)閷?yīng)的基本數(shù)據(jù)類型。清單2:Testpublic void unboxingTest() Integer i1 = 17;int i2 = 17;int i3 = 137;Integer i4 = 137;System.out.println(i1 = i2);10 System.out.println(i3 = i4);輸出:truetrue解釋下清單2第10句輸出true的原因:當(dāng)程序執(zhí)行到第10

7、句時,i4會調(diào)用IValue方法自動拆箱包裝器類型為基本數(shù)據(jù)類型。/* Returns the value of this code Integer as an* code int.*/public int intValue() return value;從源碼可以看出,當(dāng)包裝器類型和基本數(shù)據(jù)類型進行“=”比較時,包裝器類型會自動拆箱為基本數(shù)據(jù)類型。清單3內(nèi)容如下:Testpublic void unboxingTest() Integer i1 = 17;Integer i2 = 17;Integer i3 = 137;Integer i4 = 137;/ =System

8、.out.println(i1 = i2);System.out.println(i3 = i4);/ equalsSystem.out.println(i1.equals(i2);15 System.out.println(i3.equals(i4);輸出:truefalsetruetrue解釋第15句為什么會輸出true:因為在Integer包裝類實現(xiàn)的equals方法中,只要比較的當(dāng)前對象是Integer實例,那么就會自動拆箱為基本數(shù)據(jù)類型。從以下Integer類的equals方法的源碼就可看出: /* Compares this object to the specified obje

9、ct. The result is* code true if and only if the argument is not* code null and is an code Integer object that* contains the same code int value as this object.* param obj the object to compare with.* return code true if the objects are the same;* code false otherwise.*/public boolean equals(Object o

10、bj) if (obj instanceof Integer) return value = (Integer)obj).intValue();return false;Note:1. Integer、Short、Byte、Character、Long這幾個包裝類的intValue方法的實現(xiàn)是類似的;2. Double、Float的intValue方法的實現(xiàn)是類似的。3. Boolean的booleanValue方法的實現(xiàn)和intValue方法的實現(xiàn)也是類似的。裝箱拆箱綜合清單:public static void main(String args) Integer a = 1;Integer

11、 b = 2;Integer c = 3;Integer d = 3;Integer e = 321;Integer f = 321;Long g = 3L;Long h = 2L;/ 會自動拆箱(會調(diào)用intValue方法)System.out.println(c=d);/ 會自動拆箱后再自動裝箱System.out.println(e=f);/ 雖然“=”比較的是引用的是否是同一對象,但這里有算術(shù)運算,如果該引用為包裝器類型則會導(dǎo)致自動拆箱System.out.println(c=(a+b);/ equals 比較的是引用的對象的內(nèi)容(值)是否相等,但這里有算術(shù)運算,如果該引用為包裝器類型則會導(dǎo) / 致自動拆箱,再自動裝箱/ a+b觸發(fā)自動拆箱得到值后,再自動裝箱與c比較System.out.println(c.equals(a+b);/ 首先a+b觸發(fā)自動拆箱后值為int型,所以比較的是值是否相等System.out.println(g=(a+b);/ 首先a+b觸發(fā)自動拆箱后值為int型,自動裝箱后為Integer型,然后g為Long型System.out.println(g.equals(a+b);/ 首先a+h觸發(fā)自動拆箱后值為long型,因為int型的a會自動轉(zhuǎn)型為long型的g然后自動裝箱后為Long型, / 而g也為Long型System.o

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論