Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Indu Acharya
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • 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 :-)
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Narayana Bojja
Ranch Hand
Posts: 135
4
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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 :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic