当前位置:网站首页>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 .
边栏推荐
- How to clear floating?
- 1. Laravel creation project of PHP
- 你做自动化测试为什么总是失败?
- How can beginners learn flutter efficiently?
- 图像超分实验:SRCNN/FSRCNN
- 【TFLite, ONNX, CoreML, TensorRT Export】
- Pytorch MLP
- Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle
- Swift - add navigation bar
- POJ-2499 Binary Tree
猜你喜欢
Reinforcement learning - learning notes 3 | strategic learning
Sentinel sentinel mechanism of master automatic election in redis master-slave
嵌入式软件架构设计-消息交互
Error modulenotfounderror: no module named 'cv2 aruco‘
Redis cluster (master-slave) brain crack and solution
Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
One article tells the latest and complete learning materials of flutter
【TFLite, ONNX, CoreML, TensorRT Export】
[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]
Use and install RkNN toolkit Lite2 on itop-3568 development board NPU
随机推荐
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
互联网公司实习岗位选择与简易版职业发展规划
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
Video networkstate property
Get data from the database when using JMeter for database assertion
Pytorch linear regression
yolov5目標檢測神經網絡——損失函數計算原理
Hiengine: comparable to the local cloud native memory database engine
MySQL stored procedure
MySQL constraints
leetcode:1200. Minimum absolute difference
无线WIFI学习型8路发射遥控模块
【yolov5.yaml解析】
Swift - add navigation bar
Video networkState 属性
你做自动化测试为什么总是失败?
Design of music box based on assembly language
MySQL multi table operation
Intern position selection and simplified career development planning in Internet companies
一款新型的智能家居WiFi选择方案——SimpleWiFi在无线智能家居中的应用