关于Integer比较问题

本文通过一个Java代码示例,深入探讨了Integer对象在-128至127范围内的缓存机制,以及equals()和==操作符在Integer对象比较时的行为差异。代码演示了当Integer值超过127时,==比较的是引用而不是值,而equals()则始终比较值。
 1 public class Test {
 2     public static void main(String[] args) {
 3         Integer a=127;
 4         Integer b=127;
 5         System.out.println(a==b);
 6         System.out.println(a.equals(b));
 7         Integer c=128;
 8         Integer d=128;
 9         System.out.println(c==d);
10         System.out.println(c.equals(d));
11         Integer e=0;
12         Integer f=null;
13         System.out.println(e==f);
14         System.out.println(e.equals(f));
15     }
16 }

 

转载于:https://www.cnblogs.com/xQlover/p/10240722.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值