当前位置:网站首页>Integer面试解析、equals与==的区别
Integer面试解析、equals与==的区别
2022-04-21 18:01:00 【RhineHe_hjs】
Integer面试解析、equals与==的区别
我们都知道Integer是一个包装类,它是解决基本数据类型(int)不能创建对象而引入的,但是当它创建对象会有所不同,比如以下代码:
Integer integer = Integer.valueOf(100);
Integer integer2 = Integer.valueOf(100);
System.out.println(integer == integer2);//true
System.out.println(integer.equals(integer2));//true
Integer integer4 =Integer.valueOf(200);
Integer integer5 = Integer.valueOf(200);
System.out.println(integer4 == integer5);//false
System.out.println(integer4.equals(integer5));//true
-
看到创建的两个对象时100的Integer对象或许你对输出结果没有疑问,但是当你看到后面创建两个200百的Integer对象的输出结果时,你或许会好奇,为什么同样是创建的两个200Integer,他们比较却不一样呢。
在说明原因之前,我来先谈谈 == 与equals 的区别。 -
基本数据类型情况下:
也就是byte,short,char,int,long,float,double,boolean类型数据的比较,使用“==”,这是比较他们的值。 -
引用数据类型情况下:
比如说以上包装类对象、’数组、各种类对象等等,使用“==”是比较其引用的地址是不是相同,如果不相同,返回的就是false,相同返回的是true
而使用原始的equals就不一样了,equals比较分两步,不满足其中一个都返回false。首先比较两个对象的类是不是相同,比如说上面的对象都是Integer的对象,它们之间比较,第一步验证通过,如果第一步比较不同直接返回false,经过第一步验证后,进入第二步验证,比较他们之间的值,值相同就返回true,否者false。也就是说equals比较首先比较两个对象是不是同一个类创建出来的,然后比较其值。 -
说完这两种比较的区别后,我想,大家应该大致知道为什么上面的同样是数据为200的Integer对象equals比较是true了,也知道这两个对象的地址引用同了,但是对比一下上面的两个100的Integer对象,可能你的疑问又来了,所以接下来就开始解开这个疑问:在jvm运行时其实会提前加载一个字节的常用数据放在常量区,也就是-128~127这些数据,当我们创建这区间的对象时就会查看所创建的数据有没有超过这个区域,没有那么无论你创建多少个在这个数据范围内的对象,你获取的引用都是同一个,所以两个100 的Integer的对象用“==”比较是true,当你创建的对象是超过这个范围后,它便不是在这个常量池中创建了,而是重新开辟一个空间创建对象,所以两个数据为200的Integer对象的引用是不同的。
测试时所写代码
public class IntegerStaticRange {
public static void main(String[] args) {
Integer integer = Integer.valueOf(100);
Integer integer2 = Integer.valueOf(100);
System.out.println(integer == integer2);//true
System.out.println(integer.equals(integer2));//true
Integer integer4 =Integer.valueOf(200);
Integer integer5 = Integer.valueOf(200);
System.out.println(integer4 == integer5);//false
System.out.println(integer4.equals(integer5));//true
}
}
提示
以上只是我自己的看法,如有相同纯属意外。如有错误,也请谅解,勿喷!如有收获或疑问,欢迎点赞评论。
版权声明
本文为[RhineHe_hjs]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yigan123/article/details/106572811
边栏推荐
- AI+临床试验|拜耳探索创建“虚拟”对照组
- numpy.ndarray不支持逻辑运算
- Eating this open source gadget makes MCU development as efficient as Arduino
- 为什么基础设施工程师更喜欢MySQL?
- 京准GPS北斗卫星校时系统助力南亚电子生产管理系统
- 为什么switch里的case没有break不行
- Appreciation of single chip microcomputer DIY works, beginners come in to worship
- Onnx efficiency: comparison with module & dataparallel
- 有人声称「解决了」MNIST与CIFAR 10,实现了100%准确率
- What is ODBC – open database connectivity
猜你喜欢

Linux~libc. so. 6 (glibc_2.28) (64bit) is redis-5.0.3-1 nfs. x86_ 64 required

Is it really safe to outsource model training? New research: outsourcers may implant backdoors to control bank lending

单片机diy作品鉴赏,初学者进来膜拜

Zhongang Mining: a new and active application field of fluorite resources

【今晚七点】metaRTC的发展和应用场景探讨
Introduction to MySQL ODBC driver

终于有人讲明白了!原来这才是全球低时延一张网技术

【Redis】 使用Redis优化省份展示数据不显示

为什么switch里的case没有break不行

Eating this open source gadget makes MCU development as efficient as Arduino
随机推荐
MySQL query table field default value
修改van-dropdown-menu默认高度
LAXCUS分布式操作冗余容错之节点篇
离散数学 命题逻辑
Chest X-ray images - dataset
The application of new technologies has opened the second path of industrial Internet
关于无线DOS攻击的常用方法简述
What happens when the user sends a request to execute the controller method
Modify the van dropdown menu default height
看完这篇教程,你将拥有自己的一个卫星(diy全程详解)
动态规划:硬币题目总结
PMP每日一练 | 考试不迷路-4.21
Laxcus distributed operation redundancy and fault tolerance node
Look at how the technology house saves Xueba machine. I repaired my laptop with 10 yuan
信息系统项目管理师专有名词英文缩写&常见词汇
Addition, deletion, modification and query of MySQL advanced table
国泰君安安全吗?靠谱嘛
Appium source code analysis of dry goods app automated testing
【论文精读】Stable Linear Structures and Seam Measurements for Parallax Image Stitching
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena