instanceof - incompatible conditional operand types

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

The following compiles fine:

  Object o = new Object();   System.out.println(o instanceof Cloneable); 

But this doesn't:

  String s = new String();   System.out.println(s instanceof Cloneable); 

A compiler error is thrown.

What is the problem?

回答1:

A more blatant incarnation of your problem is the following:

if ("foo" instanceof Number)    // "Incompatible conditional operand types String and Number" 

This is specified in JLS 15.20.2 Type comparison operator instanceof:

RelationalExpression:        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.

That is, since this cast expression generates a compile time error:

(Number) "foo" 

so must this expression:

("foo" instanceof Number) 

Your case is a bit more subtle, but the principle is the same:

  • String is a final class
  • String does not implement Cloneable
  • Therefore you can't do (Cloneable) aString
  • Therefore also you can't do aString instanceof Cloneable


回答2:

A related issue that I have come across recently (and which led me to this page, before I figured out what was going on) is that the Eclipse environment can report "Incompatible conditional operand types" in an 'instanceof' expression erroneously due to a missing 'import' statement for the type on the right of the 'instanceof'. I spent a while trying to figure out how the types in question could possibly be incompatible before figuring out that a missing import was causing the whole problem. Hopefully this information saves somebody some time.



回答3:

The compiler knows that String is a final class and doesn't implement Cloneable. So no instance of String can ever be an instance of Cloneable. It's stopping you from thinking you've got a meaningful test when actually it will always print "false".



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!