Java instanceof - 不兼容的条件操作数类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2551337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
instanceof - incompatible conditional operand types
提问by java_geek
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?
问题是什么?
采纳答案by polygenelubricants
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
:
这在JLS 15.20.2 类型比较运算符中instanceof
指定:
RelationalExpression: RelationalExpression instanceof ReferenceType
If a cast of the RelationalExpressionto the ReferenceTypewould 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 theinstanceof
expression could never be true.
RelationalExpression: RelationalExpression instanceof ReferenceType
如果将RelationalExpression转换为ReferenceType将被拒绝作为编译时错误,那么
instanceof
关系表达式同样会产生编译时错误。在这种情况下,instanceof
表达式的结果永远不可能为真。
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 classString
does not implementCloneable
- Therefore you can't do
(Cloneable) aString
- Therefore also you can't do
aString instanceof Cloneable
String
是最后一堂课String
不执行Cloneable
- 因此你不能做
(Cloneable) aString
- 因此你也不能做
aString instanceof Cloneable
回答by Jon Skeet
The compiler knows that String
is a final class and doesn't implement Cloneable
. So no instance of String can everbe an instance of Cloneable
. It's stopping you from thinking you've got a meaningful test when actually it will always print "false".
编译器知道这String
是一个 final 类并且没有实现Cloneable
. 因此,任何 String的实例都不能成为Cloneable
. 它阻止你认为你有一个有意义的测试,而实际上它总是打印“false”。
回答by Some Guy
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.
我最近遇到的一个相关问题(在我弄清楚发生了什么之前将我带到了这个页面)是 Eclipse 环境可以错误地报告“instanceof”表达式中的“不兼容的条件操作数类型”,因为“instanceof”右侧的类型缺少“import”语句。我花了一段时间试图弄清楚有问题的类型如何可能不兼容,然后才发现丢失的导入导致了整个问题。希望这些信息可以为某人节省一些时间。