instanceof operator - why there is Illegal compile time error

后端 未结 3 1925
情书的邮戳
情书的邮戳 2021-01-06 16:19

Considering the following code, I don\'t understand why \"System.out.println( c2 instanceof D);\" will result an \"illegal compile time error\" but not return \

3条回答
  •  逝去的感伤
    2021-01-06 17:02

    Because compiler knows that casting c2 to type D will always doing to fail at runtime, and hence is marked as compile time error. So it doesn't allow such instanceof to pass through.

    Quoting JLS §15.20.2:

    If a cast (§15.16) 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.

提交回复
热议问题