aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B book page 286 table 4-1: Is the array instnaceof result correct? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B book page 286 table 4-1: Is the array instnaceof result correct?" Watch "K&B book page 286 table 4-1: Is the array instnaceof result correct?" New topic
Author

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

Joseph Zhou
Ranch Hand

Joined: Aug 01, 2000
Posts: 129
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?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Can you please post the entire code snippet so that every one can answer your question ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Joseph Zhou
Ranch Hand

Joined: Aug 01, 2000
Posts: 129
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
Brad Clarke
Greenhorn

Joined: Mar 27, 2006
Posts: 20
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

Joined: Aug 01, 2000
Posts: 129
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

Joined: Mar 27, 2006
Posts: 20
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

Joined: Aug 01, 2000
Posts: 129
Thank you Brad. Actually, that just was my question:-)
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi,

do it this way and it will compile:
Joseph Zhou
Ranch Hand

Joined: Aug 01, 2000
Posts: 129
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Aug 01, 2000
Posts: 129
Repeat my question: Is the line in the table at the page correct?
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

Joined: Aug 01, 2000
Posts: 129
Sorry Manfred, I think we are forced, otherwise there is "Object" in the table, not Foo[].
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B book page 286 table 4-1: Is the array instnaceof result correct?