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
posted
0
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
posted
0
From my point of view, we should use fooarr, not fooarr[0] here:
Thank you Brad. Actually, that just was my question:-)
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Hi,
do it this way and it will compile:
Joseph Zhou
Ranch Hand
Joined: Aug 01, 2000
Posts: 129
posted
0
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
posted
0
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
posted
0
Repeat my question: Is the line in the table at the page correct?
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
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
posted
0
Sorry Manfred, I think we are forced, otherwise there is "Object" in the table, not Foo[].