简体   繁体   中英

Overriding hashCode() and equals() methods

  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?

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 .

@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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM