当前位置:网站首页>Instance + source code = see through 128 traps
Instance + source code = see through 128 traps
2022-07-05 12:15:00 【The king of early rising】
Catalog
What is? 128 trap
What is? 128 What about the trap ? as follows :
Integer a1 = 10;
Integer a2 = 10;
System.out.println(a1 == a2); true
Integer b1 = 128;
Integer b2 = 128;
System.out.println(b1 == b2); false
You can see , The results returned are not two true. Accurately speaking , Compare according to the above form , In the interval
-128 To 127 All the results are true
Why is there this phenomenon ? Let's analyze first :
- Integer Is a wrapper class , Is a class , Reference data type .
- == Compare its address .
Thus we can see that a1 and a2 The same address as ,b1 and b2 Is different .
Source code analysis
Here is Integer An internal code , Some parts are omitted by me , Let's take a few lines to analyze .
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;
…………
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() {}
}
It's not hard to see. , The above code implements , take -127 To 128 The number of is placed in one cache Array , Is already created .
If in this range , It will directly return the address in the array created in advance . So use == To compare is equal .
Not in this range b1 and b2, Will open up new memory space .
Keep early hours , take care , The king of early rising wishes you good luck ——
sorry , I saw through your trap .
边栏推荐
- vscode快捷键
- Take you two minutes to quickly master the route and navigation of flutter
- Get data from the database when using JMeter for database assertion
- Hiengine: comparable to the local cloud native memory database engine
- Xi IO flow
- 信息服务器怎么恢复,服务器数据恢复怎么弄[通俗易懂]
- Acid transaction theory
- Embedded software architecture design - message interaction
- Redis cluster (master-slave) brain crack and solution
- JS for循环 循环次数异常
猜你喜欢
MySQL splits strings for conditional queries
yolov5目標檢測神經網絡——損失函數計算原理
多表操作-子查询
7月华清学习-1
Two minutes will take you to quickly master the project structure, resources, dependencies and localization of flutter
【TFLite, ONNX, CoreML, TensorRT Export】
MySQL storage engine
Pytorch weight decay and dropout
The survey shows that traditional data security tools cannot resist blackmail software attacks in 60% of cases
Yolov5 target detection neural network -- calculation principle of loss function
随机推荐
[singleshotmultiboxdetector (SSD, single step multi frame target detection)]
Mmclassification training custom data
7月华清学习-1
Pytorch MLP
Multi table operation - sub query
[cloud native | kubernetes] actual battle of ingress case (13)
Matlab boundarymask function (find the boundary of the divided area)
手机 CPU 架构类型了解
强化学习-学习笔记3 | 策略学习
Linux Installation and deployment lamp (apache+mysql+php)
yolov5目標檢測神經網絡——損失函數計算原理
[loss functions of L1, L2 and smooth L1]
Want to ask, how to choose a securities firm? Is it safe to open an account online?
How can beginners learn flutter efficiently?
[yolov5.yaml parsing]
[calculation of loss in yolov3]
你做自动化测试为什么总是失败?
JS for循环 循环次数异常
Hash tag usage in redis cluster
Matlab superpixels function (2D super pixel over segmentation of image)