• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object equality in Head Fisrt Java 2nd Ed.  RSS feed

 
Martin Jansen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about object equality as explained in Head First Jav 2nd edition. At page 560: explanation about 2 objects on heap but equivalent (same title so same hashcode).
Override equals:


I don't understand the '&&'. If there are 2 objects on heap, the output of foo.equals(bar) is always false?. I think the '&&' must be '||'?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No -- if foo and bar are two different objects, then "foo == bar" is false, but "foo.equals(bar)" compares their contents, not their object identities.

I am not familiar with the book, but I can tell you that "foo.equals(bar)" is not always false.

rc
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if foo.equals(bar) is true then foo.hashCode() == bar.hashCode() will also be true if the hashCode and equals methods are correctly implemented.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ralph explained, foo.equals(bar) isn't necessarily always false if foo and bar are two different objects (note that in that case, foo == bar would indeed always be false - maybe you're confusing the meaning of equals() and ==).

However, there is something strange with that if-statement.

The contract for the equals() and hashCode() methods says that if equals() returns true, then the hash codes of the two objects must be the same. (You can find more information on this in the API documentation of the equals() and hashCode() methods in class java.lang.Object).

Because of that, the second part of the expression in that if-statement is not necessary, because it would always be true if the first part of the expression is true; just this will be exactly the same:

(Unless there is a bug in the implementation of the equals() or hashCode() methods of the class of foo and bar).

(*edit* Argh, Wouter beat me! )
 
Martin Jansen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, i understand your answers, but the question was about page 560 of the Head First book. In the example there are two different objects on the heap with the same data. Then for me the equation foo.equals(bar) gives always a false (when not overridden) because in the api i found this (method equals of class Object).:
this method returns true if and only if x and y refer to the same object (x == y has the value true).


When i override the equals() and hashCode() methods then the equation can gives a 'true' but is for me redundant because you test 2 times the same.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have the Head First book, so I can't lookup what the context is on page 560 of the book.

The equals() method of class Object indeed does the same as the == operator, so if you have two different objects then it will return false, no matter if their fields have the same values.

You're not really testing the exact same thing two times; foo.equals(bar), if equals() is not overridden, just checks if foo and bar are referring to the same object (i.e., if these two reference variables point to the same address in memory). That's in principle not the same as comparing the hash codes of the objects.

But indeed, as I explained, the second part of the expression is not necessary because it will always be true if the first part is true.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page 560 is explaining that the equals and hashCode methods must be overridden together. So that code snippet is intended to demonstrate that equality and hashCode go together.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!