当前位置:网站首页>Integer internal cache
Integer internal cache
2022-07-26 13:51:00 【Teenagers】
Integer Internal Cache
stay Java in , Every time you create an object, you need to allocate memory , To reduce the frequent creation of objects , Many places use pools to store objects , Such as String String pool in , For basic types , There are caches in the corresponding wrapper classes to avoid frequent creation of objects , Such as Integer.
Integer Medium IntegerCache Static inner class :
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) {
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 j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {
}
}
The function of this class is to cache -128—127 These commonly used values , In the use of Integer.valueOf() When the method is used , We will first check whether it is within this range , If it's in this range , Point directly to the numeric address in the pool , If not, create a new object
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
But if directly new object , No checking operation
Integer it1 = new Integer(10);
Integer it2 = new Integer(10);
System.out.println(it1 == it2);
// The output is false
Integer it1 = 10 ;
Integer it2 = 10 ;
System.out.println(it1 == it2);
// true
Integer it1 = Integer.valueOf(127);
Integer it2 = Integer.valueOf(127);
System.out.println(it1 == it2);
// true
Integer it1 = Integer.valueOf(128);
Integer it2 = Integer.valueOf(128);
System.out.println(it1 == it2);
// false
Besides , You can also set vm Parameter to set the maximum value of the cache , Such as :
-Djava.lang.Integer.IntegerCache.high=2000

边栏推荐
- Thoughts on the compilation of Dr. Shuo's opening report
- Cavans realizes Static Rolling barrage
- The.Net webapi uses groupname to group controllers to render the swagger UI
- Team research and development from ants' foraging process (Reprint)
- 【黑马早报】字节旗下多款APP下架;三只松鼠脱氧剂泄露致孕妇误食;CBA公司向B站索赔4.06亿;马斯克否认与谷歌创始人妻子有婚外情...
- Tdsql-c serverless: help start-ups achieve cost reduction and efficiency increase
- 估值15亿美元的独角兽被爆裁员,又一赛道遇冷?
- Click El dropdown item/@click.native
- MySQL sets auto increment for existing primary keys
- 图书下载 | 2022年《终身监督学习导论》Meta AI、CMU等学者合著,171页PDF
猜你喜欢

大小端模式

Pytorch学习笔记(二)神经网络的使用

"Intermediate and advanced test questions": what is the implementation principle of mvcc?

白帽子揭秘:互联网千亿黑产吓退马斯克

聚力打造四个“高地”,携手合作伙伴共铸国云!

Force deduction ----- the number of words in the string
![[shaders realize overlay to re cover cross dressing effect _shader effect Chapter 9]](/img/f3/48ca9e1e8889afc0993084d6416575.png)
[shaders realize overlay to re cover cross dressing effect _shader effect Chapter 9]

Pytoch learning notes (I) installation and use of common functions

上一次听到易趣,还是上一次

「中高级试题」:MVCC实现原理是什么?
随机推荐
Tdsql-c serverless: help start-ups achieve cost reduction and efficiency increase
Book download | introduction to lifelong supervised learning in 2022, CO authored by meta AI, CMU and other scholars, 171 Pages pdf
The.Net webapi uses groupname to group controllers to render the swagger UI
LCL three-phase PWM rectifier (inverter)
Multithreaded completable future usage
Seven steps to copywriting script ---- document team collaborative management
云智技术论坛工业专场 明天见!
Code cloud, which officially supports the pages function, can deploy static pages
Docker swarm cluster builds highly available MySQL active and standby
Frisbee, 2022 "black red" top stream
【Oauth2】七、微信OAuth2授权登录
Codeforces Round #810 (Div. 2)【比赛记录】
[oauth2] v. oauth2loginauthenticationfilter
Algorithm -- continuous sequence (kotlin)
Detailed explanation of factory mode
聚力打造四个“高地”,携手合作伙伴共铸国云!
Videojs to canvas pause, play, switch video
历时15年、拥有5亿用户的飞信,彻底死了
Oom caused by improper use of multithreading
Photoshop (cc2020) unfinished