In Java development, we sometimes encounter instanceof keyword, so how to understand it?
explain
The preceding operator of the instanceof operator is a reference variable, and the latter operand is usually a class (which can be an interface) to determine whether the preceding object is the latter class, or its subclass, or an instance of the implementation class. If true is returned, otherwise false is returned. In other words:
When judging by using the instanceof keyword, the left and right operands of the instanceof operator must have an inheritance or implementation relationship.
Example
Let's use the inheritance tree to determine the return value of instanceof:
interface Man{} class Person1 implements Man{} class Student extends Person1 {} class Postgraduate extends Student {} class Animal {} public class Ex_instanceOf { public static void main(String[] args) { System.out.println("Student Whose example is the object?"); instanceofTest(new Student()); System.out.println("Animal Whose example is the object?"); instanceofTest(new Animal()); System.out.println("Postgraduate Whose example is the object?"); instanceofTest(new Postgraduate()); //An instance of a class is an instance of the class itself, of its parent class, of its parent class, and of the interface implemented. } public static void instanceofTest(Object p) { if (p instanceof Animal) System.out.println(p.getClass() + "An instance of a class is a class Animal Examples"); if (p instanceof Postgraduate) System.out.println(p.getClass() + "An instance of a class is a class Postgraduate Examples"); if (p instanceof Student) System.out.println(p.getClass() + "An instance of a class is a class Student Examples"); if (p instanceof Person1) System.out.println(p.getClass() + "An instance of a class is a class Person Examples"); if (p instanceof Man) System.out.println(p.getClass() + "An instance of a class is an interface Man Examples"); if (p instanceof Object) System.out.println(p.getClass() + "An instance of a class is a class Object Examples"); } }
In the above procedure, the inheritance tree that shows the relationships among various types is:
From the inheritance tree, we can know whether the object of a class (interface can also be regarded as a special class) is an instance of other classes (or interfaces). Just follow the arrow direction and take the class where the object is located as the starting point to reach the end point of the branch of the inheritance tree (there may be multiple branches). The classes (including this class, or interfaces) that pass along the way will do so. Both are instances of this object.
So the output is:
Whose instance is the object of Student? An instance of the class t20170722 FromInternet. Student class is an instance of the class Student An instance of the class t20170722 FromInternet. Student class is an instance of the class Person An instance of the class t20170722 FromInternet. Student class is an example of the interface Man An instance of the class t20170722 FromInternet. Student class is an instance of the class Object Whose instance is the object of Animal? Example of class t20170722 FromInternet. Animal class is an instance of class Animal An instance of class t20170722 FromInternet. Animal class is an instance of class Object Whose instance is the object of Postgraduate? An instance of the class t20170722 FromInternet. Postgraduate class is an instance of the class Postgraduate An instance of the class t20170722 FromInternet. Postgraduate class is an instance of the class Student An instance of the class t20170722 FromInternet. Postgraduate class is an instance of the class Person An instance of the class t20170722 FromInternet. Postgraduate class is an example of the interface Man An instance of the class t20170722 FromInternet. Postgraduate class is an instance of the class Object
However, one thing to note is that:
- In judging whether an object of a class (interface can also be regarded as a special class) is an instance of another class (or interface), it is necessary to first make an upward transition, and then use the instanceof keyword to make a judgment, which is the basic operational norm.
Such as:
interface A{ void say(); } class B implements A{ public void say() { System.out.println("B Realized say()Method"); } } class C implements A{ public void say() { System.out.println("C Realized say()Method"); } } public class TestDemo{ public static void main(String[] args) { A a= new B(); //Interface cannot be new System.out.println(a instanceof B); //true; A = new B (); System.out.println(a instanceof C); //false; no A = new C (); } }
The succession tree of the above relationships is as follows:
When judging whether the object a of interface A is an instance of class C or not, the use of instance of keyword will definitely return to false without the upward transformation.
Summary:
** If an instance of a class is an instance of the class itself, it is also an instance of its parent class, its parent class, and the interface implemented by it.
And the type explicitly declared by instanceof left-hand operands must be the same type as the right-hand operands or the inheritance relationship of the left-hand parent on the right **
In addition, we need to pay attention to the following two points:
boolean b5 = null instanceof String; //False; this is a special rule of instanceof: if the left operand is null, the result returns false directly, and no longer calculates what class the right operand is. boolean b4 = 'A' instanceof Character; //Compilation fails;'A'is treated here as the basic data type char, and the instanceof operator can only be used as an object's judgement.