• 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

Is any class is an array?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
I am studying the SCJP exam from Osborne book (Java2- Sun Certified Programmer & Developer for Java2), and I did not understand this idea. I wish I can find any help here.

The instanceof comparison:
With this example:
interface Face{ }
class Bar implements Face { }
class Foo extends Bar {}

The book is saying when we compare this: ( Foo[] instanceof Foo or Bar, or Face) the answer will be false. How we can set the class as an array???
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because Foo[] is instanceof Array.
Foo[index] would be instanceof Foo ect.

maybe this is a better example:


when you create and Array, new Foo[10], you do not create any Foo objects you create an Array object that hold type Foo and has a length of 10.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok Bell
If v remove

fooArray[0] = fooInstance;

then
fooArray[0] instance of Foo; //false

i am write.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the question should be wrong:

the compared object`s and the class type must be on the same class hierarchy. so
Foo[] instanceof Foo

gives a COMPILER ERROR.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramya JP:
ok Bell
If v remove

fooArray[0] = fooInstance;

then
fooArray[0] instance of Foo; //false

i am write.



yes, because fooArray[0] == null.
 
reply
    Bookmark Topic Watch Topic
  • New Topic