What is the instanceof operator used for? I\'ve seen stuff like
if (source instanceof Button) {
//...
} else {
//...
}
Best explanation is jls. Always try to check what source says. There you will get the best answer plus much more. Reproducing some parts here:
The type of the RelationalExpression operand of the instanceof operator must be a reference type or the null type; otherwise, a compile-time error occurs.
It is a compile-time error if the ReferenceType mentioned after the instanceof operator does not denote a reference type that is reifiable (§4.7).
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.