国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

java中Integer包裝類裝箱的一個(gè)細(xì)節(jié)詳解

2024-07-14 08:41:53
字體:
供稿:網(wǎng)友

前言

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ì)VeVb武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沈阳市| 黎城县| 罗定市| 黄陵县| 新密市| 共和县| 宁国市| 高邮市| 广丰县| 栾城县| 广汉市| 黑河市| 峨眉山市| 如皋市| 南郑县| 昌黎县| 和平县| 浦县| 铅山县| 泽州县| 盖州市| 滦平县| 任丘市| 云梦县| 多伦县| 永登县| 辽宁省| 奉节县| 平顺县| 承德市| 蓬安县| 密云县| 新乡县| 托里县| 安平县| 凤冈县| 托克逊县| 金阳县| 镇安县| 德清县| 新竹县|