1
  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?

6
  • What is the superclass of PC? Commented Oct 7, 2014 at 18:33
  • 3
    What does your overriden equals look like? Commented Oct 7, 2014 at 18:33
  • @PatriciaShanahan, Class Object, meaning PC has no defined superclass by me. Commented Oct 7, 2014 at 18:35
  • @Eran, I have added the overriden equals() and hashCode() in my question. Commented Oct 7, 2014 at 18:40
  • 1
    While the answer from John B is bang on, I suggest you to have a look at this for further understanding - javareferencegv.blogspot.com/2014/10/… Commented Oct 8, 2014 at 1:04

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

I didn't claim that it was the default implementation of the equals() method, though I wanted to make sure about the fact that the equals() method provided in my question is not the default one. Thank you.
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.