instanceof operator - why there is Illegal compile time error

后端 未结 3 1946
情书的邮戳
情书的邮戳 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:06

    Thats because compiler can check at compile time that such instanceof would always return false, you can read on it in JLS:

    http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#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 first operand, and ReferenceType is second: RelationalExpression instanceof ReferenceType

提交回复
热议问题