• Post Reply Bookmark Topic Watch Topic
  • New Topic

Equals() method Implementation  RSS feed

 
suraj pandhi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the program about Implementing equals() method--

I found this if statement that went directly above my head. So any one can explain this ?

if ((o instanceof Moof) && (((Moof)o).getMoofValue()== this.moofValue))

Its alright upto o instance of that is just object and class comparison and after && operator
(((Moof)o).getMoofValue()== this.moofValue))
casting object refernce and calling get method .... didn't get this ..
I will be thankful if some one can give me correct explanation of this how its working ?
 
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
if ((o instanceof Moof) && (((Moof)o).getMoofValue()== this.moofValue))

Translated to English, this means:

if the object that o refers to is a Moof object (or an instance of a subclass of Moof)
and the moofValue of that object is equal to the moofValue of the current object

(Moof)o casts o to type Moof. This is necessary because the type of the variable o is Object, and class Object doesn't have the method getMoofValue(), so you can't directly call that method on o. The cast tells the compiler: you should regard o as if it is a Moof object.

You could write the same thing out more elaborately, maybe this makes the steps more clear:


Also, note that it is not necessary to write code like this:

The expression itself already evaluates to true or false. The whole if-else around it is redundant, you could just as well write:

In your case:
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also test whether you are comparing an object with itselfThat is because every object is implicitly equal to itself, so you can miss out the rest of the method if the argument happens to be the same object.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the null check as well...
If 'o' is null then it's automatically not equal.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The null check is redundant because
obj instanceof Foo
automatically returns false if obj is null.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A search for Joshua Bloch equals recipe turns up good guidelines for overriding equals() correctly, including a recipe for overriding hashCode()
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, of course it does...oops.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interestingly enough, the link posted leads to Thinking in Java by Bruce Eckel. It also shows an error in that two identical String literals are shown as having the same hash code forgetting that they both point to the same object in the String pool.

I have a recipe for a hash code, too.The Objects class should have been available in JDK1.0.1 rather than our having to wait for Java7 for it to be written. Many people wrote their own sort of Objects classes before that.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . the same object in the String pool. . . .
Good grief! It makes me sound like those people who are practising for the OCJP exam
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!