==运算符和equals()之间有什么区别? (用hashcode()???) [英] what is the difference between == operator and equals()? (with hashcode() ???)

查看:94
本文介绍了==运算符和equals()之间有什么区别? (用hashcode()???)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更深入地学习哈希码,并认为:

I was learning hashcode in more depth and figured that:

1。如果重写equals(),则必须覆盖hashcode()。

2。要查找2个对象是否是同一个对象,请使用==运算符

考虑到这两个因素,在Java中我假设当 ==运算符用于比较如果2个实例相同或不相同,

Given those 2 factors, in Java I was assuming that when == operator is used to compare if 2 instances are same or not,

if(object1 == object2)

实际上在做什么

if(object1.hashcode() == object2.hashcode())

但是通过运行下面的测试看起来我错了。

But it appears I was wrong by running the test below.

public class Main {

    public static void main(String[] args){
        Obj1 one = new Obj1();
        Obj1 two = new Obj1();
        //is this calling hashCode() in backend???
        if(one == two) {
            System.out.println("same");
        }
        else {
            System.out.println("nope");
        }
        //this is of course return true
        if(one == one) {
            System.out.println("one and one is same");
        }
    }
}

class Obj1 {
    @Override
    public int hashCode() {
        System.out.println("hashCode() is called");
        return 111;
    }
    @Override
    public boolean equals(Object another) {
        System.out.println("equals() is called");
        return false;
    }
}

根据使用 ==运算符并查看是否等于()被调用而不是。

According to the test which uses == operator and see if equals() is called and it wasn't.

所以我的问题是如果 ==运算符可以用于比较对象是否相同,那么重写e quals的重点是什么() hashCode()比较方法?是不是 ==运营商已经完成这项工作了吗?

So my question is if == operator can used to compare if the object is same or not, what is the point of overriding equals() and hashCode() method for comparison? Isn't == operator do the job already?

参考:

重写hashCode() - 这还不错吗?

http://mindprod.com/jgloss /hashcode.html

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html# equals(java.lang.Object)

推荐答案

==运算符确定2个引用是否指向同一个对象。

the == operator determines if 2 references point to the same object.

所以

 Object o1 = new Object();
 Object o2 = o1;

 o1 == o2; //true

 o2 = new Object();

 o1 == o2 // false

Object.equals()方法是我如何确定对同一对象的2个引用是否相等?

the Object.equals() method is "how do I determine if 2 references to objects, that are not the same object, are equal?"

如果两个引用指向同一个对象,则

If two references point to the same object, both

o1 == o2 
o1.equals(o2) 

应为真。

但是如果o1和o2不是同一个对象,那么它们可能在逻辑上相等。对于任何给定的类,equals取决于对象背后的语义。例如,考虑一个类,其中field1和field2由用户设置,但是field3被计算并且具有计算的随机元素。在这种情况下,将equals定义为仅依赖于field1和field2而不是field3可能是有意义的。这就是为什么平等是必要的。

But if o1 and o2 are not the same object, they still might be equal logically. For any given class, equals depends on the semantics behind the object. For example, consider a class where field1 and field2 are set by the user, but field3 is computed and has a random element to its computation. It might make sense to define equals in this case to only depend on field1 and field2, and not field3. Thats why equals is necessary.

这篇关于==运算符和equals()之间有什么区别? (用hashcode()???)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆