Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false
来源:互联网 发布:单网络计划施工计划图 编辑:程序博客网 时间:2024/06/03 09:17
1. 为什么Integer a=34556,b=34556;但a==b为false呢?
有些人不以为然的认为,因为这是两个对象,所以肯定是false。对于上面的问题,这个回答确实没有问题。
那么若Integer a=3,b=3,则a==b是false还是true呢?
若还是按照上面的思路,那肯定也认为是false。实际上,结果为true。
为什么呢?待我娓娓道来。
这是因为Integer类在内存中有一个值的范围为[-128,127]的对象池。
只要Integer对象的值在[-128,127]范围内,都是从这个对象池中取。所以只要是这个范围内的Integer对象,只要值相同,就是同一个对象。那么==的结果,就是true。超过了这个范围,则会new新的Integer对象,尽管值相同,但是已经是不同的对象了。
验证代码如下:
public class Test2 { public static void main(String[] args) { /** * 原始数据类型的equals方法说明: Compares this object to the specified object. The * result is {@code true} if and only if the argument is not * {@code null} and is an {@code Integer} object that contains the same * {@code int} value as this object. * 意思是:原始数据类型的包装类的equals方法比较的是数值,不会比较内存地址(前提:变量不为null) * * Integer类型,有默认的对象池,范围是[-128,127]; 两个值相等的Integer类型数据a和b, 1.若值在对象池范围内 * 1)a==b结果为true 2)a.equals(b)结果为:true 2.若值不在对象池范围内 1)a==b结果为false * 2)a.equals(b)结果为:true */ //1.值相同的两个Integer类型变量与==以及equals()方法关系 Integer a = 34556; Integer b = 34556; // Integer上限 System.out.println("Integer上限:" + Integer.MAX_VALUE); // Integer类型数据的值和哈希值 System.out.println("Integer类型数据的值和哈希值"); System.out.println("a数值:" + a); System.out.println("a的哈希值:" + a.hashCode()); System.out.println("b数值:" + b); System.out.println("b的哈希值:" + b.hashCode()); System.out.println(); System.out.println("a和b的==比较结果:" + (a == b)); System.out.println("a和b的equals比较结果:" + (a.equals(b))); System.out.println("------------------------------------------------"); Integer c = 1; Integer d = 1; System.out.println("c=" + c + ",d=" + d + "---c和d的==比较结果:" + (c == d)); System.out.println("c=" + c + ",d=" + d + "---c和d的equals比较结果:" + (c.equals(d))); System.out.println("------------------------------------------------"); Integer e = 127; Integer f = 127; System.out.println("e=" + e + ",f=" + f + "---e和f的==比较结果:" + (e == f)); System.out.println("e=" + e + ",f=" + f + "---e和f的equals比较结果:" + (e.equals(f))); System.out.println("------------------------------------------------"); Integer e1 = 128; Integer f1 = 128; System.out.println("e1=" + e1 + ",f1=" + f1 + "---e1和f1的==比较结果:" + (e1 == f1)); System.out.println("e1=" + e1 + ",f1=" + f1 + "---e1和f1的equals比较结果:" + (e1.equals(f1))); System.out.println("------------------------------------------------"); Integer e2 = -128; Integer f2 = -128; System.out.println("e2=" + e2 + ",f2=" + f2 + "---e2和f2的==比较结果:" + (e2 == f2)); System.out.println("e2=" + e2 + ",f2=" + f2 + "---e2和f2的equals比较结果:" + (e2.equals(f2))); System.out.println("------------------------------------------------"); Integer e3 = -129; Integer f3 = -129; System.out.println("e3=" + e3 + ",f3=" + f3 + "---e3和f3的==比较结果:" + (e3 == f3)); System.out.println("e3=" + e3 + ",f3=" + f3 + "---e3和f3的equals比较结果:" + (e3.equals(f3))); /** * Double类型,没有默认的对象池 两个值相等的Double类型数据a和b,无论它们一起取什么值 a==b结果为false * a.equals(b)结果为:true */ System.out.println("-----------------------Double类型-------------------------"); Double e4 = 1.0; Double f4 = 1.0; System.out.println("e4=" + e4 + ",f4=" + f4 + "---e4和f4的==比较结果:" + (e4 == f4)); System.out.println("e4=" + e4 + ",f4=" + f4 + "---e4和f4的equals比较结果:" + (e4.equals(f4))); }}
测试结果
2.那么为什么所有的Integer类型且值相同的变量equals()均为true?
因为Integer包装类的equals方法,比较的是对应的int值。可以查看源码。
也可以参考一下文章《深入理解Java原始数据类型和包装类关于==和equals的比较》
这篇文章中有源码可以参考。
3.只有Integer类有对象池,其他的Short…Double都没有对象池
阅读全文
0 0
- Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false
- Integer a= 127 与 Integer b = 127
- Integer (a=127,b=127; a!=b?)
- Integer a = 128, Integer b = 128, a==b ; Integer c = 1 , integer d =1 , c==d
- python 对象池 a=1 b=1 a is b True a=257 b= 257 a is b False
- 直接a=b,对象克隆会有问题,但是为什么基本数据类型,String Integer等就没问题?
- a,b=b,a+b 与 a=b,b=a+b
- 为什么Integer a=100,b=100时候a==b返回true,而Integer c=1000,d=1000时候c==d返回false
- A^B = B^A
- A^B = B^A
- A^B = B^A
- A^B=B^A
- Python a,b=b,a+b
- 经典问题a+=b和a=a+b
- a+=b与a=a+b的不同
- a = a + b 与 a += b 的区别
- A+B=? A B均为大数
- A==B问题
- 关于TDD
- ios Youtube Data API insuffient permission的解决方案
- 出租车费
- 跨域的一点小总结。options请求出现的原因。解决方法
- 【Redis】redis的redis.conf配置文件
- Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false
- css3 border-radius画圆
- Mjpeg-streamer源码学习笔记-Main-守护进程Daemon(二)
- 九章算法--面试
- 对用户输入和程序界面进行调整
- 单链表
- [数学 二分图匹配] SRM 456 div1 FunctionalEquation
- Android NDK编译librtmp 三重奏 -- (三)编译rtmpdump
- 产品的一点总结