This week's book giveaway is in the Spring forum.
We're giving away four copies of Modern frontends with htmx and have Wim Deblauwe on-line!
See this thread for details.
Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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

Doubt in instanceof operator chapter 3 OCAJP - page# 235 (K & B OCAJP 7)

 
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have doubt in the last 3 rows of below table.
Can someone explain it.

Foo[ ] is an instanceof Object, but not an instancof Foo,Bar, Face and
Foo[1] is an instanceof Foo, Bar, Face, Object

what is the difference between Foo [ ] and Foo [1] in the example given below.

If Foo [1] instanceof FOO, Bar, Face,Object is TRUE then
why Foo[ ] instanceof Foo, Bar, Face is FALSE?





First Operand (Reference being Tested)instanceof Operand (Type we are comparing the refence against)Result
nullAny class or interface typeFALSE
Foo instanceFoo, Bar, Face, ObjectTRUE
Bar instanceBar, Face, ObjectTRUE
Bar instanceFooFALSE
Foo [ ]Foo, Bar, FaceFALSE
Foo [ ]ObjectTRUE
Foo [ 1 ]Foo, Bar, Face,ObjectTRUE[/size]



Thanks in advance :-)
 
author
Posts: 23950
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Indu Acharya wrote:
what is the difference between Foo [ ] and Foo [1] in the example given below.



Foo[] is an array of Foo. Foo[1] is element number 1 of the Foo array.

As another example, if Foo[] is a carton of eggs, then Foo[1] is an egg in slot number one. Both the egg carton and the eggs are objects, but the egg carton is certainly not an egg.

Henry
 
Indu Acharya
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Foo[] is an array of Foo. Foo[1] is element number 1 of the Foo array.

As another example, if Foo[] is a carton of eggs, then Foo[1] is an egg in slot number one. Both the egg carton and the eggs are objects, but the egg carton is certainly not an egg.



Thanks for your reply Henry.

I know the difference between Foo [ ] and FOO [1]. I was asking refering to the information provided in the table.

My question is:

If Foo [1] instanceof FOO, Bar, Face,Object is TRUE then
why Foo[ ] instanceof Foo, Bar, Face is FALSE?

 
Henry Wong
author
Posts: 23950
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Indu Acharya wrote:
If Foo [1] instanceof FOO, Bar, Face,Object is TRUE then
why Foo[ ] instanceof Foo, Bar, Face is FALSE?



I believe that I already answered this. And with a pretty good analogy, if I may say so myself...

Perhaps, if you elaborate you confusion a bit?
Henry
 
Ranch Hand
Posts: 145
4
Hibernate Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Indu,

If Foo [1] instanceof FOO, Bar, Face,Object is TRUE




Here f is referring to array(Array is also  object) of Type Foo. In this array , you can store Foo type objects. f[1] (Foo[1]) gives Foo object . So f[1] instanceof Foo or f[1] instanceof Bar or f[1] instanceof  Face gives true(Foo, Bar, Face are in IS-A relationship).

why Foo[ ] instanceof Foo, Bar, Face is FALSE?  


Actually it doesn't give false. It gives compile time error saying " Incompatible data types ".  See the below sample example to understand better.



Hope this helps !
 
Indu Acharya
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I believe that I already answered this. And with a pretty good analogy, if I may say so myself...



My bad. Got it now. Thanks :-)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic