当前位置:网站首页>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); falseYou 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 .
边栏推荐
- The evolution of mobile cross platform technology
- [calculation of loss in yolov3]
- ABAP table lookup program
- 嵌入式软件架构设计-消息交互
- Redis cluster (master-slave) brain crack and solution
- Pytorch softmax regression
- Principle of redis cluster mode
- codeforces每日5题(均1700)-第五天
- [cloud native | kubernetes] actual battle of ingress case (13)
- POJ-2499 Binary Tree
猜你喜欢

Error modulenotfounderror: no module named 'cv2 aruco‘

Mongodb replica set

报错ModuleNotFoundError: No module named ‘cv2.aruco‘

Understand kotlin from the perspective of an architect

Linux安装部署LAMP(Apache+MySQL+PHP)

强化学习-学习笔记3 | 策略学习

Matlab struct function (structure array)
你做自动化测试为什么总是失败?

Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle

MySQL splits strings for conditional queries
随机推荐
多表操作-自关联查询
II. Data type
SENT协议译码的深入探讨
Codeforces Round #804 (Div. 2)
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
MySQL multi table operation
Wireless WiFi learning 8-channel transmitting remote control module
跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
Xi IO flow
Semantic segmentation experiment: UNET network /msrc2 dataset
How to clear floating?
自动化测试生命周期
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
[yolov3 loss function]
7月华清学习-1
MySQL splits strings for conditional queries
Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
1 plug-in to handle advertisements in web pages
MySQL regular expression
Automated test lifecycle