instanceof keyword is a binary operator used to check if an object (instance) is a subtype of this type.
This is a statement that returns true if the left side of the expression is an instance of the class name on the right side.
The goal is to check the types of objects before performing any operations specific to the object, and be binary , which makes it very simple. There is also a method, but it is rarely used. For example, you can use it to write a method that checks if two randomly typed objects are compatible, for example:
public boolean areObjectsAssignable(Object left, Object right) { return left.getClass().isInstance(right); }
Please refer to this question for an additional idea,
Java operator isInstance vs instanceOf
Dish
source share