• 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

instanceof operator

 
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 hoping someone can help explain the in's and out's of using instanceof.
Given the following code:
interface alpha{}
class beta implements alpha{}
class gamma extends beta{}
public class c13
{
public static void main(String args[])
{
gamma []GA = new gamma[7];
GA[0] = new gamma();
System.out.println(GA instanceof gamma) // 1
System.out.println(GA instanceof beta ); // 2
System.out.println(GA instanceof alpha ); // 3
System.out.println(GA instanceof Object ); // 4
System.out.println(GA instanceof gamma[]); // 5
System.out.println(GA[0] instanceof gamma);// 6
System.out.println(GA[0] instanceof beta );// 7
System.out.println(GA[0] instanceof alpha);// 8
System.out.println(GA[0] instanceof Object);// 9
}
} // close class
When compiling I would expect the whole code to compile and give the results:
false//1
false//2
false//3
true//4
true//5
true//6
true//7
true//8
true//9
However whilst compiling I get the following error:
inconvertible types
found : gamma[]
required: gamma
System.out.println(GA instanceof gamma);
inconvertible types
found : gamma[]
required: gamma
System.out.println(GA instanceof beta);
inconvertible types
found : gamma[]
required: gamma
System.out.println(GA instanceof alpha);
This relates to the first three lines of the instanceof comparison.
When I comment out the 3 offending lines - the code then runs. But I can't understand why the first three lines will not compile! Any ideas why this is happening?
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java compiler is pretty smart. If you have an expression "A instanceof B" and the compiler can determine that A can never, ever be an instance of B, it will give you a compile-time error message. In virtually all cases, you will find that you wrote such code while not fully understanding the problem and, if the compiler hadn't complained, you would've had a serious bug in your application. So that's definitely a good thing.
In this case, a variable referencing an array of gamma objects can never contain a reference to a plain (non-array) gamma object. So the compiler complains.
See if you understand this.With the classes defined above, and the following variables:Then the following statements are true:
  • "a instanceof B" will compile, because the variable "a" can certainly hold a reference to a B object.
  • "b instanceof A" will compile as well, but it would be equivalent to "b != null".
  • "b instanceof C" will fail to compile because b can never contain a reference to a C object.
  • The language specification puts it like this: for an expression "RelationalExpression instanceof ReferenceType", 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. In other words, because b can never contain a C, "(C)b" is a compile-time error, and therefore "b instanceof C" is a compile-time error as well.
    Still, you can make your code work by cheating the compiler a little. All you have to do is turn GA into an Object reference, so that it can potentially contain any class.Does that help?
    - Peter
    [ February 12, 2003: Message edited by: Peter den Haan ]
     
    Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic