This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes instance of problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instance of problem" Watch "instance of problem" New topic
Author

instance of problem

in.anurag Singh
Greenhorn

Joined: May 14, 2010
Posts: 25
Hi,
There is an example in the book of SCJP study guide, which is not entering into my mind..


In this example the equals() method is overridden as above, but i am not able to understand how (o instanceof Moof ) is true.
I want to know if the "o" is of Moof Type or the Object Type..?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

"o" is of type Object, but the method checks in the first part of the expression in "if" whether the reference gets passed in to the method is of type "Moof".


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
in.anurag Singh
Greenhorn

Joined: May 14, 2010
Posts: 25
if "o" is of type Object then,
how (o instanceOf Moof ) is true,
because
(Parent insanceOf child) is illegal.
I am just confusing, please help..and explain..
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

if "o" is of type Object then, how (o instanceOf Moof ) is true,

No, "o" in here is a method local variable, think of it as a place holder. At the runtime the code calling this method passes a reference which might/might not be of type "Moof". Hence it should be verified before proceed further. Read more about variables here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
For the instanceof operator, try the Java™ Language specification, page 320 and page 385. Note it returns false if a null value is passed.
Note also thatis very poor programming style when you can simply writeNote also that if you find a copy of Effective Java™ by Joshua Bloch or Angelika Langer's paper you will see how the instanceof operator can potentially be unreliable in the equals() method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: instance of problem
 
Similar Threads
Want hepl to understand Implementin an equals()
instanceof
Doubt about instanceof operator
Generics and collections K&B pg 526
Not able to understand the program