Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sapana jain
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic