- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- java中Integer包裝類(lèi)裝箱的示例分析
這篇文章主要為大家展示了“java中Integer包裝類(lèi)裝箱的示例分析”,內容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習一下“java中Integer包裝類(lèi)裝箱的示例分析”這篇文章吧。
前言
java有八個(gè)基本數據類(lèi)型,每個(gè)都有對應的一個(gè)包裝類(lèi),比如int對應的Integer。 Integer 是int的包裝類(lèi)型,數據類(lèi)型是類(lèi),初值為null,從jdk1.5開(kāi)始,java引入了自動(dòng)拆裝箱,可以直接進(jìn)行形如Integer i = 20
形式的賦值,編譯器會(huì )自動(dòng)將其轉換為Integer i = Integer.valueOf(20)
進(jìn)行裝箱,拆箱則是將int j = i的形式轉換成了int j = i.intValue()
。
裝箱有個(gè)細節,如果不注意很容易出錯,來(lái)看一下:
Integer i = 20; Integer j = Integer.valueOf(20); System.out.println(i == j);
上面的代碼輸出為
true
好像沒(méi)什么問(wèn)題,那我們形式不變,將數字20換成200,即
i = 200; j = Integer.valueOf(200); System.out.println(i == j);
同樣的判斷,輸出變成了:
false
這是為什么呢?
先明確一點(diǎn),經(jīng)過(guò)編譯器編譯后,Integer i = 20
轉換成了Integer i = Integer.valueOf(20)
,和Integer j = Integer.valueOf(20)
的定義完全一樣,那為什么將20換成了200后判斷結果不一樣了呢?
我們來(lái)看看Integer.valueOf(int i)
方法的內部:
public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
可以看出當i在某個(gè)區間內時(shí),直接返回了緩存數組IntegerCache.cache
中的一個(gè)值,超出區間才new一個(gè)新的Integer對象。到這里我們大概就可以得出結論:20在緩存范圍內所以直接用了緩存,但是200超出了緩存區間所以new了新對象,和原來(lái)對象的地址當然不會(huì )相同,所以返回false
再來(lái)看看IntegerCache,這是一個(gè)Integer的私有靜態(tài)內部類(lèi),定義如下:
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) { int i = parseInt(integerCacheHighPropValue); 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() {} }
可以看出默認的緩存區間是-128~127,那么什么情況下會(huì )修改這個(gè)范圍呢,修改了某個(gè)虛擬機參數的時(shí)候,通過(guò)代碼也可看出,設置的這個(gè)緩存上限java.lang.Integer.IntegerCache.high
值不能小于127,小于的話(huà)就會(huì )被賦予127,從而失效。
那么這個(gè)值怎么設置呢?我們來(lái)看看jdk源碼中怎么解釋IntegerCache這個(gè)靜態(tài)內部類(lèi):
Cache to support the object identity semantics of autoboxing for values between -128 and 127 (inclusive) as required by JLS. The cache is initialized on first usage. The size of the cache may be controlled by the -XX:AutoBoxCacheMax= option. During VM initialization, java.lang.Integer.IntegerCache.high property may be set and saved in the private system properties in the sun.misc.VM class.
大概意思是:
將-128到127(包含)的數字做緩存以供自動(dòng)裝箱使用。緩存在第一次使用時(shí)被初始化。大小可以由JVM參數-xx:autoboxcachemax=option來(lái)指定。JVM初始化時(shí)此值被設置成java.lang.Integer.IntegerCache.high屬性并作為私有的系統屬性保存在sun.misc.vm.class中。
可以得到結論:這個(gè)緩存的high值是由JVM參數 -XX:AutoBoxCacheMax= option來(lái)指定的。
上述jdk源碼來(lái)源于jdk1.7,不同版本實(shí)現略有不同,但思路一致。
這種共享常用對象的思路有一個(gè)名字,叫享元模式,英文名叫Flyweight,即共享的輕量級元素。其他包裝類(lèi)如Boolean、Byte、Short、Long、Charactor都有類(lèi)似的實(shí)現。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站