jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instanceof operator" Watch "instanceof operator" New topic

instanceof operator

Tony Virdee

Joined: Jan 21, 2003
Posts: 6
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:
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?
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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 ]
    I agree. Here's the link: http://aspose.com/file-tools
    subject: instanceof operator
    It's not a secret anymore!