当前位置:网站首页>Why is it best to use equals for integer comparisons
Why is it best to use equals for integer comparisons
2022-06-26 00:41:00 【White as dust】
List of articles
Why? Integer It is suggested to use equals Well ? It's rare “==” Is it not fragrant
1. int And int Comparison
int As 8 One of the basic types , Values are allocated on the stack . And the basic type does not provide equals The way , Only use “==”.
2. Integer And int Comparison
Integer Objects are allocated in the heap , When comparing, you need to use equals Do you ? It doesn't need to ,“==” It still smells good .
because “Integer ==int” Is equivalent to Integer.intValue() ==int Of .
public static void main(String[] args) {
System.out.println("5==5: " + test(5,5));
System.out.println("127==127: " + test(127,127));
System.out.println("128==128: " + test(128,128));
}
static boolean test(Integer num1, int num2){
return num1 == num2;
}

3. Integer And Integer Comparison
static boolean test(Integer num1, Integer num2){
return num1 == num2;
}

You can see 5 and 127 Comparison ,“==” It still smells good . actual -128 To 127 stay Integer.IntegerCache There is a cache in the array of , So all in this range Integer The objects of are all the same , That is, the reference address is the same .
4. Conclusion
Integer And Integer It is better to use equals, After all, I don't know when ,Integer The value of is not in that range .
Is it possible to use int no need Integer Well ? No way ,int Default initialization value bit 0,Integer by null.
边栏推荐
- "Method not allowed", 405 problem analysis and solution
- Explain from a process perspective what happens to the browser after entering a URL?
- Precautions for cleaning PCBA board in SMT chip processing
- Explain the synchronize keyword
- 【ROS进阶篇】第一讲 常用API介绍
- Wireshark's analysis of IMAP packet capturing
- 实现异步的方法
- CaMKIIa和GCaMP6f是一樣的嘛?
- mtb13_Perform extract_blend_Super{Candidate(PrimaryAlternate)_Unique(可NULL过滤_Foreign_index_granulari
- QT custom QSlider with cursor
猜你喜欢

Qt优秀开源项目之九:qTox

Performance leads the cloud native database market! Intel and Tencent jointly build cloud technology ecology

Flink reports error: a JNI error has occurred, please check your installation and try again
![Mysql5.7 is in the configuration file my Ini[mysqld] cannot be started after adding skip grant tables](/img/b2/2b87b3cea1422e2a860f5e0e7dcc40.png)
Mysql5.7 is in the configuration file my Ini[mysqld] cannot be started after adding skip grant tables

What are the red lines of open source that should not be trodden on?

EBS R12.2.0升级到R12.2.6

机器视觉:照亮“智”造新“视”界

元宇宙中的法律与自我监管

Atlas200dk brush machine

Flink报错:Error: A JNI error has occurred, please check your installation and try again
随机推荐
Redis cluster
实现异步的方法
darkent2ncnn出错
Understanding of prototypes and prototype chains
毕业季 | 在不断探索中拟合最好的自己
Redis memory elimination mechanism
Flink报错:Error: A JNI error has occurred, please check your installation and try again
SQL按某字段去重 保留按某个字段排序最大值
Comprehensive introduction to Simulink solver
JS to input the start time and end time, output the number of seasons, and print the corresponding month and year
Solution to component stele in SMT chip processing
SSL unresponsive in postman test
How to bypass SSL authentication
Atlas200dk brush machine
DNS review
Law and self-regulation in the meta universe
Qt之自定义带游标的QSlider
dbca静默安装及建库
1-10Vmware构建自定义的网络架构
Solution to SMT grape ball phenomenon