詳解Java 自動(dòng)裝箱與自動(dòng)拆箱
包裝器
有些時(shí)候,我們需要把類似于int,double這樣的基本數(shù)據(jù)類型轉(zhuǎn)成對(duì)象,于是設(shè)計(jì)者就給每一個(gè)基本數(shù)據(jù)類型都配置了一個(gè)對(duì)應(yīng)的類,這些類被稱為包裝器。
包裝器整體來說分為四大種:
Number,Number類派生出了Integer,Double,Long,F(xiàn)loat,Short,Byte這六個(gè)小類分別代表了int,double,long,float,short,byte這六種基本數(shù)據(jù)類型。 Character,對(duì)應(yīng)的基本數(shù)據(jù)類型是char。 Void,對(duì)應(yīng)的是關(guān)鍵字void,這個(gè)類我們會(huì)經(jīng)常在反射中看到,用于表示方法的返回值是void,這里不再贅述,后面反射章節(jié)詳細(xì)講解。 Boolean,對(duì)應(yīng)的是基本數(shù)據(jù)類型boolean。要記住下面兩點(diǎn)包裝器的特性:
包裝器是不可變的,一旦構(gòu)造了包裝器,就不允許更改包裝在其中的值。
包裝器是final定義的,不允許定義它的子類。自動(dòng)裝箱和自動(dòng)拆箱
ArrayList<Integer> list = new ArrayList<>();list.add(3);int x = list.get(0);
自動(dòng)裝箱
當(dāng)我們添加int值 到一個(gè)集合元素全部是Integer的集合中去時(shí)候,這個(gè)過程發(fā)生了什么?
list.add(3);//實(shí)際上面的代碼會(huì)被編譯器給自動(dòng)的變成下面的這個(gè)代碼list.add(Integer.valueOf(3))
編譯器在其中所作的這個(gè)事情就叫做自動(dòng)裝箱。
自動(dòng)拆箱
當(dāng)我們?nèi)〕鲆粋€(gè)集合中的元素并將這個(gè)元素賦給一個(gè)int類型的值的時(shí)候,這其中又發(fā)生了什么呢?
int x = list.get(0);//實(shí)際上面的代碼會(huì)被編譯器給自動(dòng)的變成下面的這個(gè)代碼int x = list.get(0).intValue();
編譯器這其中所作的這個(gè)事情就叫做自動(dòng)拆箱
自動(dòng)裝箱和自動(dòng)拆箱中的坑
Integer i1 = 100;Integer i2 = 100;Integer i3 = 300;Integer i4 = 300;System.out.println(i1 == i2);System.out.println(i3 == i4);
這是一道經(jīng)典的面試題,打印出來的結(jié)果是:
truefalse
為什么會(huì)發(fā)生這樣的事情,我們記得自動(dòng)裝箱的時(shí)候會(huì)自動(dòng)調(diào)用Integer的valueOf方法,我們現(xiàn)在來看一下這個(gè)方法的源碼:
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i);}
而這個(gè)IntegerCache是什么呢?
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() {}}
從這2段代碼可以看出,在通過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ì)象。
這樣我們就不難理解為什么一個(gè)是false,一個(gè)是true了。
其他的包裝器的valueOf方法也有不同的實(shí)現(xiàn)和不同的范圍,具體的我們會(huì)在源碼深度解析專欄來分析,敬請(qǐng)期待~
以上就是詳解Java 自動(dòng)裝箱與自動(dòng)拆箱的詳細(xì)內(nèi)容,更多關(guān)于Java 自動(dòng)裝箱與自動(dòng)拆箱的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. Ajax返回值類型與用法實(shí)例分析3. Vue element ui用戶展示頁面的實(shí)例4. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)5. 如何在jsp界面中插入圖片6. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)7. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼8. css代碼優(yōu)化的12個(gè)技巧9. asp批量添加修改刪除操作示例代碼10. .NET6打包部署到Windows Service的全過程
