java中Integer包裝類裝箱的一個(gè)細(xì)節(jié)詳解
前言
java有八個(gè)基本數(shù)據(jù)類型,每個(gè)都有對(duì)應(yīng)的一個(gè)包裝類,比如int對(duì)應(yīng)的Integer。 Integer 是int的包裝類型,數(shù)據(jù)類型是類,初值為null,從jdk1.5開始,java引入了自動(dòng)拆裝箱,可以直接進(jìn)行形如Integer i = 20
形式的賦值,編譯器會(huì)自動(dòng)將其轉(zhuǎn)換為Integer i = Integer.valueOf(20)
進(jìn)行裝箱,拆箱則是將int j = i的形式轉(zhuǎn)換成了int j = i.intValue()
。
裝箱有個(gè)細(xì)節(jié),如果不注意很容易出錯(cuò),來看一下:
Integer i = 20; Integer j = Integer.valueOf(20); System.out.println(i == j);
上面的代碼輸出為
true
好像沒什么問題,那我們形式不變,將數(shù)字20換成200,即
i = 200; j = Integer.valueOf(200); System.out.println(i == j);
同樣的判斷,輸出變成了:
false
這是為什么呢?
先明確一點(diǎn),經(jīng)過編譯器編譯后,Integer i = 20
轉(zhuǎn)換成了Integer i = Integer.valueOf(20)
,和Integer j = Integer.valueOf(20)
的定義完全一樣,那為什么將20換成了200后判斷結(jié)果不一樣了呢?
我們來看看Integer.valueOf(int i)
方法的內(nè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); }
可以看出當(dāng)i在某個(gè)區(qū)間內(nèi)時(shí),直接返回了緩存數(shù)組IntegerCache.cache
中的一個(gè)值,超出區(qū)間才new一個(gè)新的Integer對(duì)象。到這里我們大概就可以得出結(jié)論:20在緩存范圍內(nèi)所以直接用了緩存,但是200超出了緩存區(qū)間所以new了新對(duì)象,和原來對(duì)象的地址當(dāng)然不會(huì)相同,所以返回false
再來看看IntegerCache,這是一個(gè)Integer的私有靜態(tài)內(nè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() {} }
可以看出默認(rèn)的緩存區(qū)間是-128~127,那么什么情況下會(huì)修改這個(gè)范圍呢,修改了某個(gè)虛擬機(jī)參數(shù)的時(shí)候,通過代碼也可看出,設(shè)置的這個(gè)緩存上限java.lang.Integer.IntegerCache.high
值不能小于127,小于的話就會(huì)被賦予127,從而失效。
那么這個(gè)值怎么設(shè)置呢?我們來看看jdk源碼中怎么解釋IntegerCache這個(gè)靜態(tài)內(nè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(包含)的數(shù)字做緩存以供自動(dòng)裝箱使用。緩存在第一次使用時(shí)被初始化。大小可以由JVM參數(shù)-xx:autoboxcachemax=option來指定。JVM初始化時(shí)此值被設(shè)置成java.lang.Integer.IntegerCache.high屬性并作為私有的系統(tǒng)屬性保存在sun.misc.vm.class中。
可以得到結(jié)論:這個(gè)緩存的high值是由JVM參數(shù) -XX:AutoBoxCacheMax= option來指定的。
上述jdk源碼來源于jdk1.7,不同版本實(shí)現(xiàn)略有不同,但思路一致。
這種共享常用對(duì)象的思路有一個(gè)名字,叫享元模式,英文名叫Flyweight,即共享的輕量級(jí)元素。其他包裝類如Boolean、Byte、Short、Long、Charactor都有類似的實(shí)現(xiàn)。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java的Jackson庫的使用及其樹模型的入門學(xué)習(xí)教程
這篇文章主要介紹了Java的Jackson庫的使用及其樹模型入門學(xué)習(xí)教程,Jackson庫通常被用來作Java對(duì)象和JSON的互相轉(zhuǎn)換,需要的朋友可以參考下2016-01-01SpringBoot中GlobalExceptionHandler異常處理機(jī)制詳細(xì)說明
Spring Boot的GlobalExceptionHandler是一個(gè)全局異常處理器,用于捕獲和處理應(yīng)用程序中發(fā)生的所有異常,這篇文章主要給大家介紹了關(guān)于Java中GlobalExceptionHandler異常處理機(jī)制的相關(guān)資料,需要的朋友可以參考下2024-03-03java實(shí)現(xiàn)任意矩陣Strassen算法
這篇文章主要介紹了java實(shí)現(xiàn)任意矩陣Strassen算法的相關(guān)資料,需要的朋友可以參考下2016-02-02Spring中的FactoryBean與BeanFactory詳細(xì)解析
這篇文章主要介紹了Spring中的FactoryBean與BeanFactory詳細(xì)解析,在Spring框架中,FactoryBean和BeanFactory是兩個(gè)關(guān)鍵的接口,用于創(chuàng)建和管理對(duì)象實(shí)例,它們?cè)赟pring的IoC(Inversion of Control,控制反轉(zhuǎn))容器中發(fā)揮著重要的作用,需要的朋友可以參考下2023-11-11Java之如何正確地對(duì)包裝類進(jìn)行裝箱與拆箱
在這篇文章中給大家繼續(xù)講解包裝類的裝箱和拆箱問題。你可能會(huì)很好奇,做java開發(fā),怎么還裝起箱子來了?那么就請(qǐng)大家?guī)е苫笸驴窗?/div> 2023-04-04SpringSecurity中的EnableWebSecurity注解啟用Web安全詳解
這篇文章主要介紹了SpringSecurity中的EnableWebSecurity注解啟用Web安全詳解,@EnableWebSecurity是Spring?Security用于啟用Web安全的注解,典型的用法是該注解用在某個(gè)Web安全配置類上,實(shí)現(xiàn)了接口,需要的朋友可以參考下2023-12-12最新評(píng)論