当前位置:网站首页>128 traps - source code analysis
128 traps - source code analysis
2022-06-22 03:40:00 【Yu who wants to fly】
128 trap —— Source code analysis
`public static void main(String[] args) {
Integer aInteger = 101;
// Automatic boxing Integer.valueOf(101)
Integer bInteger = 101;
System.out.println(aInteger==bInteger);
Integer cInteger = 1001;
Integer dInteger = 1001;
System.out.println(cInteger==dInteger);
int a = 101;
// Automatic dismantling aInteger.intValue()
System.out.println(a==aInteger);
int b = 1001;
System.out.println(b==cInteger);
//Integer.valueOf(b)
System.out.println(dInteger==b);
}`
The result of the above code is :
Why are the first two answers different ? This is it. Java Of 128 trap .
128 trap : Automatic packing specification requirements boolean\byte\char<=127, Be situated between -128~127 Between short and int Wrapped in a fixed object .
Why the range is -128~127 Between ? Let's take a look at the source code .
Auto boxing call function Integer.valueOf(101);
`@HotSpotIntrinsicCandidate
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}`
static final int low = -128;
static final int high;
static final Integer[] cache;
static Integer[] archivedCache;
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
h = Math.max(parseInt(integerCacheHighPropValue), 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
// Load IntegerCache.archivedCache from archive, if possible
VM.initializeFromArchive(IntegerCache.class);
int size = (high - low) + 1;
// Use the archived cache if it exists and is large enough
if (archivedCache == null || size > archivedCache.length) {
Integer[] c = new Integer[size];
int j = low;
for(int i = 0; i < c.length; i++) {
c[i] = new Integer(j++);
}
archivedCache = c;
}
cache = archivedCache;
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {
}
}`
You can see in the i>=-128&&i<=127 when , Take the data directly from the cache array and return , There is no need to create a new object . Not in this range , The new Integer object , So the address points to different , That's when true and false The situation of . In the next three true in , It is because of the automatic unpacking operation .
边栏推荐
- Analyzing iceberg merge tasks to resolve data conflicts
- Simple introduction to thoroughly understand anti shake and throttling
- 2022.6.21-----leetcode.1108
- Threejs realizes the fluctuation hot spot effect, fluctuation mark and fluctuation label display
- svn与cvs的区别有哪些
- 倍福 PLC 字符串类型string操作
- 128陷阱——源码分析
- [qnx hypervisor 2.2 user manual]5.5 starting and using guest
- 3de 保存到收藏夹
- 3DE recover from design
猜你喜欢

docker 安装redis

倍福TwinCAT3伺服控制常用功能块的实现

Scheduling function: Splunk operator Controller Manager

我们如何解决了RealSense偏色问题?

泛微 E-cology V9 信息泄露漏洞

利用yolov5训练自己的数据集; yolov5的安装与使用 ; yolov5源码解读

MySQL 45 lecture notes (I) execution of an SQL statement

(problem solving) missing gcr io/kubebuilder/kube-rbac-proxy:v0.8.0

美容院怎样做活动

Rabbmitmq simple mode < 1 >
随机推荐
ASUS reinstallation system keyboard lamp failure = & gt; Refitting the ATK drive
Introduction and use of kubernetes
倍福TwinCAT3第三方伺服电机——以汇川IS620N伺服为例子
C51的一些日记
倍福CX9020(WINCE 7)控制器使用方法和操作
rabbmitMQ 简单模式<一>
Join等待多个线程的同步结果
LOCAL=NO
gdb调测使用整理
VIM common commands
Cloud native architecture (02) - what is cloud native
Nebula Graph学习篇2_版本v2.6.1之前的bug导致OOM
webview报错
Summary of image classification based on pytoch: swing transformer
3DE position of moving object
多线程 interrupt用法
倍福Twincat NC PTP使用介绍
R data analysis: significance and practice of calibration curve and DCA curve in clinical prediction model
76. 最小覆盖子串-滑动窗口法
3DE recover from design