当前位置:网站首页>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 .
边栏推荐
- 【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
- Video networkState 属性
- A guide to threaded and asynchronous UI development in the "quick start fluent Development Series tutorials"
- byte2String、string2Byte
- Redis master-slave mode
- 无线WIFI学习型8路发射遥控模块
- Semantic segmentation experiment: UNET network /msrc2 dataset
- Select drop-down box realizes three-level linkage of provinces and cities in China
- 只是巧合?苹果 iOS16 的神秘技术竟然与中国企业 5 年前产品一致!
- [upsampling method opencv interpolation]
猜你喜欢
![[cloud native | kubernetes] actual battle of ingress case (13)](/img/1a/9404f6dcedd15827fa45f8f6f4c093.png)
[cloud native | kubernetes] actual battle of ingress case (13)

Network five whip

Why learn harmonyos and how to get started quickly?
![[yolov5.yaml parsing]](/img/ae/934f69206190848ec3da10edbeb59a.png)
[yolov5.yaml parsing]

How to clear floating?

Uniapp + unicloud + Unipay realize wechat applet payment function

7月华清学习-1

Read and understand the rendering mechanism and principle of flutter's three trees
Take you two minutes to quickly master the route and navigation of flutter

Get data from the database when using JMeter for database assertion
随机推荐
Open3d mesh (surface) coloring
Multi table operation - sub query
Understand kotlin from the perspective of an architect
The evolution of mobile cross platform technology
Seven ways to achieve vertical centering
Simple production of wechat applet cloud development authorization login
Halcon 模板匹配实战代码(一)
查看rancher中debug端口信息,并做IDEA Remote Jvm Debug
Halcon template matching actual code (I)
[untitled]
【load dataset】
Pytorch linear regression
Application of a class of identities (vandermond convolution and hypergeometric functions)
Codeforces Round #804 (Div. 2)
MySQL storage engine
Matlab label2idx function (convert the label matrix into a cell array with linear index)
Pytorch softmax regression
【TFLite, ONNX, CoreML, TensorRT Export】
Matlab boundarymask function (find the boundary of the divided area)
无线WIFI学习型8路发射遥控模块