• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

instanceof

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In K&B 1.4 book at page no 159 given

interface Face{}
class Bar implements Face{}
class Foo extends Bar{}

and the table shows

doubt 1)Foo[] instanceof Foo/Bar/Face ---->false

But,according to me Foo[] instanceof Anyclass Except Object gives result �compile error�(not False)
Please clarify.

Doubt 2)Foo[1] instanceof Foo/Bar/Face/Object----true
Can anyone please explain what the author is trying to explain from the above line

Foo[] f1=new Foo[]{new Foo()};
System.out.println(f1[1] instanceof Foo);//at compile time (Foo)f1[1] meaning (Foo)Foo[](LHS/referencetype) .My doubt is,array and Foo class having what connection to tell that compiletime is ok.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shiva Mohan:
...according to me Foo[] instanceof Anyclass Except Object gives result �compile error�(not False)...


I don't have the book, so I'm not sure what the table is showing (whether this is an error or not), but you are correct that a compiler error will result.

According to JLS - 15.20.2 Type Comparison Operator instanceof...

If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.


The last sentence of this quote might be grounds for saying Foo[] instanceof Foo/Bar/Face is "false" -- even though the code will never run.

The reason for the compiler error is that Foo[] is not a Foo, but instead is an array of Foo references.

In contrast, Foo[x] (where x is an int denoting the array index) is an element of the array -- that is, it's a Foo reference. Therefore, Foo[x] instanceof Foo, Bar, Face, or Object all return true.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me give this a shot since i'm studying the same book...


the first one doesn't work because you are comparing an array, which instanceof works in this case only against Object. Array is a type of Object..but in the first doubt1 you aren't comparing it against object, only the other 3 classes.

the second one works because, like marc says, it's not Array Object being compared anymore, it's an actual reference of type of Foo which is the child of all 4 classes.
[ May 26, 2007: Message edited by: Michael Raymond Jr. ]
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic