instanceof - 不兼容的条件操作数类型

发布于 2024-08-27 09:00:13 字数 264 浏览 6 评论 0原文

以下代码可以正常编译:

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

但这则不然:

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

会抛出编译器错误。

问题是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习ぎ惯性依靠 2024-09-03 09:00:13

我最近遇到的一个相关问题(在我弄清楚发生了什么之前,它引导我进入此页面)是,由于以下原因,Eclipse 环境可能会在“instanceof”表达式中错误地报告“不兼容的条件操作数类型”缺少“instanceof”右侧类型的“import”语句。我花了一段时间试图弄清楚有问题的类型如何可能不兼容,然后才发现缺少导入导致了整个问题。希望这些信息可以节省一些时间。

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.

∞梦里开花 2024-09-03 09:00:13

您的问题的更明显的体现如下:

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

这是在 JLS 15.20.2 类型比较运算符 instanceof

关系表达式:
       关系表达式引用类型实例

如果将 RelationalExpression 转换为 ReferenceType 会因编译时错误而被拒绝,则 instanceof 关系表达式同样会产生编译时错误。在这种情况下,instanceof 表达式的结果永远不可能为 true。

也就是说,由于此强制转换表达式会生成编译时错误:

(Number) "foo"

因此此表达式也必须:

("foo" instanceof Number)

您的情况有点微妙,但原理是相同的:

  • String 是一个最终类
  • String< /code> 没有实现 Cloneable
  • 因此你不能做 (Cloneable) aString
  • 因此你也不能做 aString instanceof Cloneable

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
假情假意假温柔 2024-09-03 09:00:13

编译器知道 String 是最终类,并且不实现 Cloneable。因此,String 的实例永远都不能是Cloneable 的实例。它阻止你认为你已经进行了有意义的测试,而实际上它总是打印“false”。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文