This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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


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: 3899
    
    9

"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: 3899
    
    9

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: 43970
    
  33
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.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: instance of problem
 
It's not a secret anymore!