关于new integer(1)==new integer(1)

先上测试代码:

复制代码
public class  Test{
    public static void main(String[] args)
    {
        Integer l1 = new Integer(1);
        Integer l2 = new Integer(1);

        System.out.println(l1==l2);
        System.out.println(l1==1);
        //System.out.println(B.c);
    }
}
复制代码

 

 解释:

Integer 是引用类型,l1==l2的含义是,对比两个引用类型是否相同,换句话说就是两一个引用是否引用同一个对象。由于各自进行了new所以肯定不是同一个对象地址,所以输出false.

l1==1,先进行拆箱,得到基本类型int及数值1,与另一个进行对比。如果不信,则可以运行:

Integer l3 = new Integer(2000);
System.out.println(l3==2000);

输出也是true.

Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2);

输出true

在-128~127之间,进行数字缓存,对比的则是之间的缓存。

 

posted @   Shapley  阅读(339)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2021-11-24 mybatis知识汇集
点击右上角即可分享
微信分享提示