Loading…
The instanceof operator in Java is used to test whether an object is an instance of a particular class, interface, or a subclass/interface of a given type. It returns a boolean value indicating whether the object on the left-hand side is an instance of the specified type. Syntax: object instanceof type object: The object to be tested. type: The class or interface to check against. Example: class Animal {} class Dog extends Animal {} class Cat extends Animal {} public class InstanceOfExample { public static void main(String[] args) { Animal myDog = new Dog(); Animal myCat = new Cat(); // Using instanceof to check object types System.out.println("Is myDog a Dog? " + (myDog instanceof Dog)); // true System.out.println("Is myDog a Cat? " + (myDog instanceof Cat)); // false System.out.println("Is myCat a Do...