This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP  ::: chapter 4 p.no 286 instanceof .......... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP  ::: chapter 4 p.no 286 instanceof .........." Watch "SCJP  ::: chapter 4 p.no 286 instanceof .........." New topic
Author

SCJP ::: chapter 4 p.no 286 instanceof ..........

sapana jain
Ranch Hand

Joined: Oct 24, 2007
Posts: 42
interface Face { }
class Bar implements Face{ }
class Foo extends Bar { }


First Operand instanceof Operand Result
Reference Being Tested) (Type We�re Comparing
the Reference Against)
----------------------------------------------------------------------------
1.null Any class or interface false
type

2.Foo instance Foo, Bar, Face, Object true

3.Bar instance Bar, Face, Object true

4.Bar instance Foo false

5.Foo [ ] Foo, Bar, Face false

6.Foo [ ] Object true

7.Foo [ 1 ] Foo, Bar, Face, Object true


in last 3 points i am getting totally confused........if in 5. point result is false then how in 7. point it become true...........???
Raphael Kou
Greenhorn

Joined: Oct 23, 2007
Posts: 13
Maybe in line 5 it means the Array Object. So an "Array Object of Foos" is not a Foo, Bar etc.

In line 7 you get a concrete Element out of the Foo Array which is a Foo Object
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
Can you place the question in a better a way?

But if your getting confused with hirarchy than i'll suggest you to draw the class hirarchy and then check the paths if class and the object being tested come under same path and in same or above branch than instance of will return true.

e.g. if C extends B, B extends A, D extends A
in this object of C is instance of B as well as A. While B's object is instanceof A but not C and D is having no relation with C and B in instanceof operator.


http://subodh-gupta.blogspot.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP ::: chapter 4 p.no 286 instanceof ..........
 
Similar Threads
Objective 7.6 Java Operators (instanceof) Operator
instanceof
instanceOf explanation needed
K&B book page 286 table 4-1: Is the array instnaceof result correct?
instanceof