File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP  ::: chapter 4 286 instanceof .......... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP  ::: chapter 4 286 instanceof .........." Watch "SCJP  ::: chapter 4 286 instanceof .........." New topic

SCJP ::: chapter 4 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

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

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.
I agree. Here's the link:
subject: SCJP ::: chapter 4 286 instanceof ..........
It's not a secret anymore!