Instanceof is a Java two-dollar operator, and ==,>,< is the same kind of stuff. Because it is made up of letters, it is also a reserved keyword for java. Its purpose is to test whether the object on its left is an instance of the class to the right of it, returning a Boolean type of data
The instanceof operator in Java is used to indicate at run time whether an object is an instance of a particular class. Instanceof Indicates whether this object is an instance of this particular class or its subclasses 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:
Copy CodeThe code is as follows:
Package com.instanceoftest;
Interface a{}
Class B implements a{
}
Class C extends B {
}
Class Instanceoftest {
public static void Main (string[] args) {
A A=null;
B B=null;
Boolean res;
System.out.println ("Instanceoftest test Case 1:------------------");
res = a instanceof A;
System.out.println ("A instanceof A:" + res);
res = b instanceof B;
System.out.println ("b instanceof B:" + res);
System.out.println ("\ninstanceoftest test Case 2:------------------");
A=new B ();
B=new B ();
res = a instanceof A;
System.out.println ("A instanceof A:" + res);
res = a instanceof B;
System.out.println ("A instanceof B:" + res);
res = b instanceof A;
System.out.println ("b instanceof A:" + res);
res = b instanceof B;
System.out.println ("b instanceof B:" + res);
System.out.println ("\ninstanceoftest test Case 3:------------------");
B b2= (c) New C ();
res = B2 instanceof A;
System.out.println ("B2 instanceof A:" + res);
res = B2 instanceof B;
System.out.println ("B2 instanceof B:" + res);
res = B2 instanceof C;
System.out.println ("B2 instanceof C:" + res);
}
}
/*
Result
Instanceoftest test Case 1:------------------
A instanceof A:false
b instanceof B:false
Instanceoftest test Case 2:------------------
A instanceof A:true
A instanceof B:true
b instanceof A:true
b instanceof B:true
Instanceoftest test Case 3:------------------
B2 instanceof A:true
B2 instanceof B:true
B2 instanceof C:true
Instanceof are typically used to invoke different methods based on different instances:
In a class with inheritance, we can invoke different methods in different instances by polymorphism:
Example 1:
There are three classes, class names, and the relationships between them are as follows
Animal (superclass) Dog (subclass) Cat (subclass)
The following objects can be obtained
Animal Animal =new Animal (); = = = = "Animal instanceof Animal Returns True
Dog Dog=new Dog (); = = = = "Dog instanceof Dog Returns True
Cat Cat=new cat () = = = = Cat Instanceof Cat Returns True
Animal dog=new Dog () = = = = "Dog instanceof Animal Returns True
Animal cat=new cat () = = = "Cat instanceof Animal return True
1 2Animal dog=NewDog ();3Animal cat=NewCat ();4 5List List =NewArrayList ();6 7 List.add (dog);8 List.add (cat);9 TenIterator it =list.iterator (); One while(It.hasnext ()) { A It.next (). Animaldo (); - -}
Here we can override the Animaldo method in animal in the dog and cat classes by calling the Animaldo method, and then automatically calling methods in different instances based on the instance.
Second, in a class without inheritance, we can judge the current instance by instanceof, and then invoke different methods according to different instances:
Example 2:
1Station S =NewStation ();2Cell C =NewCell ();3 4 5List List =NewArrayList ();6 7 List.add (s);8 List.add (c);9 Ten OneIterator it =list.iterator (); A while(It.hasnext ()) { -Object obj =It.next (); - if(objinstanceofStation ) { theStation S1 =(station) obj; - S1.stationdo (); - } - if(objinstanceofCell) { +Cell C1 =(Cell) obj; - C1.celldo (); + } A}
Here we can judge the result by instanceof, execute the corresponding action method in the non-homogeneous (Stationdo (), Celldo ()).
Generally in the use of non-generic collections (List, set, etc.), more use of instanceof, because the collection can save a variety of objects, so in the read generally to make appropriate judgments.
Summary of usage of instanceof keywords in Java