当前位置:网站首页>Caching mechanism for wrapper types
Caching mechanism for wrapper types
2022-06-24 10:23:00 【Ugly and ugly】
Java Most of the wrapper types corresponding to the basic data types use the caching mechanism to improve performance .Byte、Short、Integer、Long this 4 The wrapper class creates a value by default [-128, 127] The corresponding type of cache data of the closed interval ,Character The packing type creates a value in [0, 127] Cache data in a closed range ,Boolean Packaging type direct return True perhaps False.
Byte Cache source code :
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
private static class ByteCache {
private ByteCache(){}
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}Short Cache source code :
public static Short valueOf(short s) {
final int offset = 128;
int sAsInt = s;
if (sAsInt >= -128 && sAsInt <= 127) { // must cache
return ShortCache.cache[sAsInt + offset];
}
return new Short(s);
}
private static class ShortCache {
private ShortCache(){}
static final Short cache[] = new Short[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}Integer Cache source code :
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(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) {
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() {}
}
You can see , Through valueOf() Method creation Integer When the object , If the value is [-128,127] Between , And then it goes back to IntegerCache.cache[] A reference to an object that already exists in ; Otherwise create a new Integer object .
Long Cache source code :
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
private static class LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}Character Cache source code :
public static Character valueOf(char c) {
if (c <= 127) { // must cache
return CharacterCache.cache[(int)c];
}
return new Character(c);
}
private static class CharacterCache {
private CharacterCache(){}
static final Character cache[] = new Character[127 + 1];
static {
for (int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}
You can see , Through valueOf() Method creation Character When the object , If the value is [0,127] Between , And then it goes back to CharacterCache.cache[] A reference to an object that already exists in ; Otherwise create a new Character object .
Boolean Cache source code :
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
Two types of floating-point wrapper classes Float、Double There is no caching mechanism .
besides , Note the comparison of values between all integer wrapper class objects , All required equals Methods to compare . This is a mandatory requirement in Alibaba code specification .
This article references from :Java Basic and common knowledge & Interview question summary ( On ) | JavaGuide
边栏推荐
- 5. dish management business development
- 使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
- 微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)
- 2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
- numpy.logical_and()
- Development of anti fleeing marketing software for health products
- leetCode-1089: 复写零
- Record the range of data that MySQL update will lock
- Troubleshooting steps for Oracle pool connection request timeout
- YOLOv6:又快又准的目标检测框架开源啦
猜你喜欢

Resolved: methods with the same name as their class will not be constructors in

Three ways to use applicationcontextinitializer

uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。

3.员工的增删改查

YOLOv6:又快又准的目标检测框架开源啦

SVG+js拖拽滑块圆形进度条

415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)

Phpstrom code formatting settings

JMeter接口测试工具基础— 取样器sampler(二)

SQL Sever中的窗口函数row_number()rank()dense_rank()
随机推荐
被困英西中学的师生安全和食物有保障
分布式事务原理以及解决分布式事务方案
Learning to organize using kindeditor rich text editor in PHP
How does home office manage the data center network infrastructure?
414-二叉树的递归遍历
oracle池式连接请求超时问题排查步骤
Uniapp implements the function of clicking to make a call
学习使用phpstripslashe函数去除反斜杠
用扫描的方法分发书稿校样
Troubleshooting steps for Oracle pool connection request timeout
How to customize sharing links in wechat for H5 web pages
2. login and exit function development
416 binary tree (first, middle and last order traversal iteration method)
学习使用KindEditor富文本编辑器,点击上传图片遮罩太大或白屏解决方案
Error reading CSV (TSV) file
Getting user information for applet learning (getuserprofile and getUserInfo)
5.菜品管理业务开发
3. addition, deletion, modification and query of employees
Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
uniapp实现点击拨打电话功能