Java的Integer緩存池用法
Java的Integer緩沖池?
Integer 緩存池主要為了提升性能和節(jié)省內(nèi)存。根據(jù)實踐發(fā)現(xiàn)大部分的數(shù)據(jù)操作都集中在值比較小的范圍,因此緩存這些對象可以減少內(nèi)存分配和垃圾回收的負(fù)擔(dān),提升性能。
在-128到 127范圍內(nèi)的 Integer 對象會被緩存和復(fù)用。
原理
int 在自動裝箱的時候會調(diào)用Integer.valueOf,進而用到了 IntegerCache。
@HotSpotIntrinsicCandidate
public static Integer value0f(int i){
if(i>= IntegerCache.low && i<= IntegerCache.high) //如果傳入的int值在緩存范圍內(nèi),則直接從緩存中返回Integer對象
return IntegerCache.cache[i+(-IntegerCache.low)];
return new Integer(i); //否則,創(chuàng)建新的Integer對象
}
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 = VM.getSavedProperty( key:"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 i = low;
for(int k=0;k< cache.length; k++) //遍歷創(chuàng)建-128-127的所有對象
cache[k]= new Integer(i++);
assert IntegerCache.high >= 127;
}
private IntegerCache(){}
}所以這里還有個面試題:就是為啥 Integer 127 之內(nèi)的相等,而超過 127 的就不等了?
因為小于等于127的 Integer 對象是從同一個緩存池中獲取的,它們指向的是相同的對象實例,所以它們的引用相等
不僅 Integer 有,Long 同樣有一個緩存池,不過范圍是寫死的 -128 到 127,不能通過JVM參數(shù)進行調(diào)整
@HotSpotIntrinsicCandidate
public static Long value0f(long l){
final int offset = 128;
if(l>= -128 &&l<= 127){ // will cache
return LongCache.cache[(int)l + offsetl];
}
return new Long(l);
}總結(jié)
- Byte,Short,Integer,Long這4種包裝類默認(rèn)創(chuàng)建了數(shù)值[-128,127]的相應(yīng)類型的緩存數(shù)據(jù)
- Character 創(chuàng)建了數(shù)值在 [0,127]范圍的緩存數(shù)據(jù)
- Boolean 直接返回 True or False
- Float 和 Double 沒有緩存數(shù)據(jù),畢竟是小數(shù),能存的數(shù)太多了
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用hutool工具進行導(dǎo)入導(dǎo)出excel表格
如何在后臺添加導(dǎo)入導(dǎo)出表格的功能呢,本期的文章將會帶領(lǐng)小伙伴們一起實現(xiàn)此功能,文中有詳細(xì)的代碼示例和圖文介紹,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10
Java mysql詳細(xì)講解雙數(shù)據(jù)源配置使用
在開發(fā)過程中我們常常會用到兩個數(shù)據(jù)庫,一個數(shù)據(jù)用來實現(xiàn)一些常規(guī)的增刪改查,另外一個數(shù)據(jù)庫用來實時存數(shù)據(jù)。進行數(shù)據(jù)的統(tǒng)計分析??梢宰x寫分離??梢愿玫膬?yōu)化和提高效率;或者兩個數(shù)據(jù)存在業(yè)務(wù)分離的時候也需要多個數(shù)據(jù)源來實現(xiàn)2022-06-06
SpringBoot整合jnotify實現(xiàn)針對指定目錄及其(動態(tài))子目錄的監(jiān)聽的方法
本文介紹了JNotify這一Java庫在SpringBoot中的應(yīng)用,JNotify允許應(yīng)用程序監(jiān)聽文件系統(tǒng)事件,包括文件夾/文件的創(chuàng)建、刪除、修改和重命名,由于JNotify底層調(diào)用的關(guān)鍵部分是C語言開發(fā)的,所以在使用前需要在系統(tǒng)中加入相應(yīng)的動態(tài)庫2024-10-10
Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
本篇文章主要介紹了Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
springboot+redis實現(xiàn)訂單過期(超時取消)功能的方法詳解
在Spring Boot中使用Redis實現(xiàn)訂單過期(超時取消)功能,有多種成熟方案,本文為大家整理了幾個詳細(xì)方法,文中的示例代碼講解詳細(xì),大家可以根據(jù)需要進行選擇2025-12-12
SpringBoot?中使用?Validation?校驗參數(shù)的方法詳解
Validation?是用于檢查程序代碼中參數(shù)的有效性的框架,作為?Spring?框架中的一個參數(shù)校驗工具,集成在?spring-context?包中,這篇文章主要介紹了SpringBoot?中使用?Validation?校驗參數(shù),需要的朋友可以參考下2022-05-05

