当前位置:网站首页>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.
边栏推荐
- Flink reports error: a JNI error has occurred, please check your installation and try again
- Compiler Telegram Desktop end (tdesktop) en utilisant vs2022
- tensorrt pb转uff问题
- mtb13_ Perform extract_ blend_ Super{candidate (primaryalternate) \u unique (nullable filtering \foreign\index\granulati
- "Seamless" deployment of paddlenlp model based on openvinotm development kit
- Research and development practice of Kwai real-time data warehouse support system
- Ffmpeg version switching
- Setting up a cluster environment under Linux (2) -- installing MySQL under Linux
- Atlas200dk刷机
- Types of feeder and how to work
猜你喜欢
mtb13_ Perform extract_ blend_ Super{candidate (primaryalternate) \u unique (nullable filtering \foreign\index\granulati
【ROS进阶篇】第一讲 常用API介绍
Research and development practice of Kwai real-time data warehouse support system
Display unassigned virtual address after easyconnect connection
什么是微服务
How to deliver a shelter hospital within 48 hours?
快手实时数仓保障体系研发实践
idea设置mapper映射文件的模板
Compile the telegraph desktop side (tdesktop) using vs2022
Apache基金会正式宣布Apache InLong成为顶级项目
随机推荐
1-11Vmware虚拟机常见的问题解决
ORA-01153 :激活了不兼容的介质恢复
SQL中只要用到聚合函数就一定要用到group by 吗?
【TSP问题】基于Hopfield神经网络求解旅行商问题附Matlab代码
ffmpeg 版本切换
信号处理函数内必须使用可重入函数
Display unassigned virtual address after easyconnect connection
Camkiia et gcamp6f sont - ils les mêmes?
《SQL优化核心思想》
How ASA configures port mapping and pat
Compile the telegraph desktop side (tdesktop) using vs2022
Causes and solutions to the phenomenon of PCBA monument in SMT patch processing
[TSP problem] solving traveling salesman problem based on Hopfield neural network with matlab code
CaMKIIa和GCaMP6f是一樣的嘛?
快手实时数仓保障体系研发实践
yolov5 提速多GPU训练显存低的问题
no_ Expand and use_ concat
Redis cluster
11.1.2 overview of Flink_ Wordcount case
leetcode. 14 --- longest public prefix