What is the instanceof keyword, and what does it do?
Ans : The instanceof keyword is a two-argument operator that tests whether the run-time type of its first argument is assignment compatible with its second argument. An expression using instanceof, such as X instanceof Y, tests whether the object referred to by X could be assigned to a variable of type Y. If Y is a class, this test checks whether X’s object belongs to class Y or to a subclass of Y. If Y is an interface, the test checks if X’s object’s class implements that interface. The instanceof operator performs tests at both compile time and run time. For example, testing whether a String instance in a String variable can be an instance of Integer fails at compile time: String aString = “abadcafe”; if (aString instanceof Integer) { /* … */ } The error message from the JDK (1.0.2 and 1.1) compiler is unequivocal: Impossible for java.lang.String to be instance of java.lang.Integer. Failure at compile time signals gross errors, such as checking two class types, neither of which is