Un'incarnazione più evidente del problema è il seguente:
if ("foo" instanceof Number)
// "Incompatible conditional operand types String and Number"
Questo è specificato 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.
Cioè, dal momento che questa espressione getto genera un errore di compilazione:
(Number) "foo"
così deve questa espressione:
("foo" instanceof Number)
Il tuo caso è un po 'più sottile, ma il principio è lo stesso:
String
è una classe finale
String
non implementa Cloneable
- Quindi si può 't do
(Cloneable) aString
- Pertanto non è possibile fare
aString instanceof Cloneable
se si utilizza eclissi, dare un'occhiata alla risposta SomeGuys. – Christian