25
class A { public static void main(String...args) { Integer var = 10; if(var instanceof Character) // Line1 System.out.println("var is a Character"); } } 

I know Line 1 will not compile because the compiler has found that var is not a Character.

What I fail to understand is why the compiler throws an error instead of returning false or true.

If the compiler returns false or true (i.e treating the instanceof operation like a regular if-based validation), then it be much more useful.. would it not?

Or am I missing something obvious?

4
  • 3
    Change Integer var = 10; to Object var = 10;. The compiler is telling you that var can never be instanceof Character if you declare it an Integer. Commented Aug 31, 2013 at 13:05
  • I suppose if you want something similar to an instanceof check but instead of throwing an error it will return false, you could try Character.class.isInstance(var) which will return false in this case. Commented Aug 31, 2013 at 13:19
  • 2
    @JoshM see my comment on the other answer. The JLS specifies that an error is thrown for a good reason. If you just had to write that var is an Integer why on earth would you then want to check that it isn't? Unless you have an exceptionally short memory... Commented Aug 31, 2013 at 13:23
  • @BoristheSpider True, I don't think he is concerned with how straightforward his example is (you could obviously see that var is an Integer so the instanceof check is obviously pointless) but I think he just wants to generally know the reasoning behind it. Commented Aug 31, 2013 at 13:25

2 Answers 2

23

It's a compilation error in accordance with JLS §15.20.2:

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.

RelationalExpression is the first operand of instanceof and ReferenceType is the second.

Sign up to request clarification or add additional context in comments.

Comments

3

In addition to the arshajii's answer, if you want to avoid compile-time error and want run-time true/false result for checking whether var is instance of Character, then use code like this:

if(var.getClass().isAssignableFrom(Character.class)) System.out.println("var is a Character"); else System.out.println("var is NOT a Character"); 

As you would expect it will print:

var is not a Character

2 Comments

I think the fundamental point of the JLS is that this check can never return true if you know that the type is not assignable. This is entirely pointless - if you know it's an Integer then this check is redundant, return false would work just as well. If you do not know that the var is an Integer, i.e. you have an Object, then instanceof will compile just fine.
it's useful, if I want do less code in some case. I have crosswalk project in shared mode at develop phase and a embedded mode for production, I use if (this instanceof XWalkActivity) to check, but it compile error. https://crosswalk-project.org/documentation/shared_mode.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.