一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - java中Integer包裝類裝箱的一個細節詳解

java中Integer包裝類裝箱的一個細節詳解

2021-05-21 11:08沉默哥 Java教程

Java中的Integer是int的包裝類型,下面這篇文章主要給大家介紹了關于java中Integer包裝類裝箱的一個細節的相關資料,文中介紹的這個細節挺重要的,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起

前言

java有八個基本數據類型,每個都有對應的一個包裝類,比如int對應的integer。 integer 是int的包裝類型,數據類型是類,初值為null,從jdk1.5開始,java引入了自動拆裝箱,可以直接進行形如integer i = 20形式的賦值,編譯器會自動將其轉換為integer i = integer.valueof(20)進行裝箱,拆箱則是將int j = i的形式轉換成了int j = i.intvalue()

裝箱有個細節,如果不注意很容易出錯,來看一下:

?
1
2
3
4
integer i = 20;
integer j = integer.valueof(20);
 
system.out.println(i == j);

上面的代碼輸出為

true

好像沒什么問題,那我們形式不變,將數字20換成200,即

?
1
2
3
4
i = 200;
j = integer.valueof(200);
 
system.out.println(i == j);

同樣的判斷,輸出變成了:

false

這是為什么呢?

先明確一點,經過編譯器編譯后,integer i = 20轉換成了integer i = integer.valueof(20) ,和integer j = integer.valueof(20)的定義完全一樣,那為什么將20換成了200后判斷結果不一樣了呢?

我們來看看integer.valueof(int i)方法的內部:

?
1
2
3
4
5
6
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在某個區間內時,直接返回了緩存數組integercache.cache中的一個值,超出區間才new一個新的integer對象。到這里我們大概就可以得出結論:20在緩存范圍內所以直接用了緩存,但是200超出了緩存區間所以new了新對象,和原來對象的地址當然不會相同,所以返回false

再來看看integercache,這是一個integer的私有靜態內部類,定義如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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,那么什么情況下會修改這個范圍呢,修改了某個虛擬機參數的時候,通過代碼也可看出,設置的這個緩存上限java.lang.integer.integercache.high值不能小于127,小于的話就會被賦予127,從而失效。
那么這個值怎么設置呢?我們來看看jdk源碼中怎么解釋integercache這個靜態內部類:

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(包含)的數字做緩存以供自動裝箱使用。緩存在第一次使用時被初始化。大小可以由jvm參數-xx:autoboxcachemax=option來指定。jvm初始化時此值被設置成java.lang.integer.integercache.high屬性并作為私有的系統屬性保存在sun.misc.vm.class中。

可以得到結論:這個緩存的high值是由jvm參數 -xx:autoboxcachemax= option來指定的。

上述jdk源碼來源于jdk1.7,不同版本實現略有不同,但思路一致。

這種共享常用對象的思路有一個名字,叫享元模式,英文名叫flyweight,即共享的輕量級元素。其他包裝類如boolean、byte、short、long、charactor都有類似的實現。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://www.cnblogs.com/JackPn/p/9392145.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 极品主播的慰在线播放 | 成人男女网免费 | 91香蕉导航 | 国产激情一区二区三区四区 | 5x社区在线观看直接进入 | 色婷婷综合久久久中文字幕 | 亚洲国产欧美在线人网站 | 国产精品自在线拍 | 亚洲精品AV无码喷奶水糖心 | 帅小伙和警官同性3p | 九草视频在线 | 性欧美4khdxxxx | 亚洲人成伊人成综合网久久 | 国产精品www | 欧美va免费大片 | 男插女的下面免费视频夜色 | 国产麻豆成91 | 国产免费资源高清小视频在线观看 | 亚洲福利一区二区 | 韩国久播影院理论片不卡影院 | 四虎国产一区 | 午夜理论片日本中文在线 | 香蕉久久久久久狠狠色 | 美女的让男人桶爽网站 | 超级乱淫伦短篇小说做车 | 亚洲男女天堂 | 精品久久99麻豆蜜桃666 | 亚洲精品一区二区久久久久 | 国产色视频网站 | 97就去干 | 国产高清在线播放免费观看 | 久久精品视频91 | 亚洲AV中文字幕无码久久 | 亚洲国产精品成 | 亚洲经典激情春色另类 | 日韩欧免费一区二区三区 | 国产福利资源 | 亚洲欧美国产自拍 | 国产精品久久毛片完整版 | 亚洲白拍 | 亚洲美日韩 |