欢迎访问我的个人网站==》 jiashubing.cn

Java的equals方法,首先要判断类型是否相同

如下代码,Long 和Integer 进行比较:

Integer aa = 1;
Long bb= 1L;
System.out.println(aa.equals(bb));

输出为:false

查看Integer 的equals 方法

  public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

equals方法,首先要判断类型是否相同,重写的时候也应该考虑这一点

这是基础啊,别再忘了。。。

posted @ 2018-05-18 15:35  贾树丙  阅读(559)  评论(0编辑  收藏  举报