The instanceof operator is a two-dollar operator (operator) of Java, php, and = =, >, < is the same class of things. Because it is made up of letters, it is also a reserved keyword for java. Its function is to determine whether its left object is an instance of its right class and returns a Boolean type of data. Can be used to determine whether an instance of a subclass in an inheritance is an implementation of a parent class. Equivalent to the IS operator in C #. 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.
Usage:
Result = Object Instanceof class
Parameters:
Result: Boolean type.
Object: Required option. An arbitrary object expression.
Class: Required option. Any of the defined object classes.
Description
If object is an instance of class, the instanceof operator returns TRUE. Returns False if object is not an instance of the specified class, or if object is null.
Examples are as follows:
Packagecom.instanceoftest;Interfacea{}classBImplementsa{}classCextendsB {}classInstanceoftest { Public Static voidMain (string[] args) {A a=NULL; b b=NULL; BooleanRes; System.out.println ("Instanceoftest test Case 1:------------------"); Res= AinstanceofA; System.out.println ("A instanceof A:" +res); Res= binstanceofB; System.out.println ("B instanceof B:" +res); System.out.println ("\ninstanceoftest test Case 2:------------------"); A=NewB (); b=NewB (); Res= AinstanceofA; System.out.println ("A instanceof A:" +res); Res= AinstanceofB; System.out.println ("A instanceof B:" +res); Res= binstanceofA; System.out.println ("B instanceof A:" +res); Res= binstanceofB; System.out.println ("B instanceof B:" +res); System.out.println ("\ninstanceoftest test Case 3:------------------"); B B2= (C)NewC (); Res= B2instanceofA; System.out.println ("B2 instanceof A:" +res); Res= B2instanceofB; System.out.println ("B2 instanceof B:" +res); Res= B2instanceofC; System.out.println ("B2 instanceof C:" +res); }}/*result:instanceoftest test Case 1:------------------a instanceof a:falseb instanceof b:falseinstanceoftest test CAs E 2:------------------a instanceof A:truea instanceof b:trueb instanceof a:trueb instanceof b:trueinstanceoftest test Case 3:------------------B2 instanceof a:trueb2 instanceof b:trueb2 instanceof c:true
The use of instanceof in Java