面试题:a.equals(1) && a.equals(2) && a.equals(3)为true,为什么

文章展示了如何使用Java反射API访问`cache`字段并修改其内容,随后在equals方法中观察到意外的结果,这揭示了关于对象相等性检查的一个细节。
摘要由CSDN通过智能技术生成

1.背景

    public static void main(String[] args) throws Exception {
        Class cache = Integer.class.getDeclaredClasses()[0];
        Field c = cache.getDeclaredField("cache");
        c.setAccessible(true);
        Integer[] array = (Integer[]) c.get(cache);
        array[130] = 1;
        array[131] = 1;

        Integer a = 1;
        if (a.equals(1) && a.equals(2) && a.equals(3)) {
            System.out.println("true");
        }
    }

输出:true

2.为什么呢

debug一下:看图,瞬间明白了

完美!

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值