• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...........???
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Slime does not pay. Always keep your tiny ad dry.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic