• 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

K&B book page 286 table 4-1: Is the array instnaceof result correct?

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 5 in the table says: Foo[] object instanceof Foo, Bar, Face, result is false. Actually, it should fail to compile. Did I miss anything?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please post the entire code snippet so that every one can answer your question ?
 
Joseph Zhou
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is:

Table 4-1 summarizes the use of the instanceof operator given the following:
interface Face { }
class Bar implements Face{ }
class Foo extends Bar { }

table 4-1 Operands and Results Using instanceof Operator.

First Operand instanceof Operand Result
(Reference Being Tested) (Type We�re Comparing the
Reference Against)
Foo[] Foo, Bar, Face false
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I came up with that compile clean and gives the expected results as indicated in table 4-1. No idea if this is the type of code they had intended to result in the output though.

 
Joseph Zhou
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From my point of view, we should use fooarr, not fooarr[0] here:

System.out.println("Foo[] instanceof Foo: " + (fooarr[0] instanceof Foo));
System.out.println("Foo[] instanceof Bar: " + (fooarr[0] instanceof Bar));
System.out.println("Foo[] instanceof Face: " + (fooarr[0] instanceof Face));

Just as in:
System.out.println("Foo[] instanceof Object: " + (fooarr instanceof Object));

Because both are Foo[].
 
Brad Clarke
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe so, but the code won't compile if you do that.

D:\Development\Java5\InstanceTest.java:28: inconvertible types
found : Foo[]
required: Foo
System.out.println("Foo[] instanceof Foo: " + (fooarr instanceof Foo));
 
Joseph Zhou
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Brad. Actually, that just was my question:-)
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

do it this way and it will compile:
 
Joseph Zhou
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you to find a way to make it compile-able, but it is not what I want to say.

When you look at the table:

First Operand_____________|instanceof Operand________|Result
(Reference Being Tested)__|(Type We�re Comparing the_|
__________________________|Reference Against)________|
Foo[]_____________________| Foo, Bar, Face___________| false

it's the Foo[] object, not Object, instanceof Foo, Bar, Face, get result false.

I think this is not a correct result, because you can't pass the compile.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joseph Zhou:
it's the Foo[] object, not Object, instanceof Foo, Bar, Face, get result false.

I think this is not a correct result, because you can't pass the compile.


I think you confuse two things.

The instanceof check, which will be correct even if you use a parent type for the reference variable, and the compile error which will always occur if there is no relation between the classes. For example:
The compilation will fail, since the compiler knows that it is impossible that foo IS A Bar.
 
Joseph Zhou
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Repeat my question: Is the line in the table at the page correct?
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joseph Zhou:
Repeat my question: Is the line in the table at the page correct?

Yes, since nobody forces you to use a reference variable of type Foo[]. For the test you need only an object of type Foo[].
 
Joseph Zhou
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Manfred, I think we are forced, otherwise there is "Object" in the table, not Foo[].
 
reply
    Bookmark Topic Watch Topic
  • New Topic