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都没有对象池

原创粉丝点击