The instanceof operator in Java is used to indicate at run time whether an object is an instance of a particular class. Instanceof is indicated by returning a Boolean value,
Whether this object is an instance of this particular class or its subclasses. The previous operand of the instanceof operation is usually a reference type, and the latter
The operand is usually a class (which can be an interface). He is used to determine whether the preceding object is a later class, or a subclass, to implement an instance object of the class, with a +,-etc
The arithmetic operator usage is roughly the same.
Examples are as follows:
Public classInstanceoftest { Public Static voidMain (string[] agrs) {Object o= "Hello"; //Output TrueSystem.out.println ("Whether the string is an object instance:" +oinstanceofObject); //Output TrueSystem.out.println ("is string instance:" +oinstanceofString); String s= "World"; //compilation fails, string and math have no inheritance relationshipSystem.out.println ("Whether the string is a math instance:" +sinstanceofMath); }}
Usage of instanceof in Java