wood burning stoves*
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
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: 3817

"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: 3817

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: 37991
    
  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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: instance of problem
 
Similar Threads
Not able to understand the program
Want hepl to understand Implementin an equals()
instanceof
Doubt about instanceof operator
Generics and collections K&B pg 526