Overriding hashCode() and equals() methods

邮差的信 提交于 2020-05-17 03:09:49

问题


  1. I override both the hashCode() and equals(), but I don't modify anything inside the overridden methods.

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + Objects.hashCode(this.model);
        hash = 67 * hash + this.year;
        return hash;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PC other = (PC) obj;
        if (!Objects.equals(this.model, other.model)) {
            return false;
        }
        if (this.year != other.year) {
            return false;
        }
        return true;
    }
    
  2. I create 2 identical objects:

    PC One = new PC();
    
    One.setModel("HP");
    
    One.setYear(2013);
    
    PC Two = new PC();
    
    Two.setModel("HP");
    
    Two.setYear(2013);
    
  3. I compare those 2 objects:

     if (One.equals(Two)) {
    
            System.out.println("They are the same objects!");
    
      } else {
    
           System.out.println("They are different objects!");
       }
    

The result is: "They are the same objects!". However, if I don't override both methods, the result will be: "They are different objects!". Because the hashCode is unique for each object (I suppose), I have expected thet the result to be: "They are different objects!". Q: Why?


回答1:


The default equals implementation of Object uses the instance's reference address. Two Objects are equal ONLY if they reside at the same location in memory.

If you don't override equals that is the implementation you get.

Also, this behavior has nothing to do with hashCode is you are not calling hashCode. If you call equals directly there is not use of hashCode. hashCode is generally used in data structures just as HashMap.




回答2:


@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final PC other = (PC) obj;
    if (!Objects.equals(this.model, other.model)) {
        return false;
    }
    if (this.year != other.year) {
        return false;
    }
    return true;
}

This method you claim to be the default implementation of the equals method is not true. Equals defaults to comparison via the == equality operator which compares the 32 or 64 bit pointer references to JVM memory locations. Check this link

public boolean equals(Object obj) {
       return (this == obj);
}



回答3:


When you don't override equals, your PC objects inherits the equals method from Object, which performs the same thing as == -- comparing object references to see if they are the same object.

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

So, your equals method returns true after comparing the internal contents of your PC objects, but when not overridden, Object's equals method returns false because they are different objects.



来源:https://stackoverflow.com/questions/26242832/overriding-hashcode-and-equals-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!