To discriminate whether a class and an instance have an implementation relationship, usually using instanceof and class<?>.isinstanceof (obj), we need to know the difference between the two in order to be able to use the 2 approach better.
Usually we use the following relationship, instanceof illustrates the direct implementation relationship of the class and the object if (obj instanceof objclass) {//. }//How this relationship is implemented//first, if obj is an instance of objclass, then obj must be non-null, so obj!=null, second, Obj.getclass () ==objclass.class//so is equivalent to if (obj!= Null && obj.getclass () ==objclass.class) {//}//use = = instead of equal, because this is more reasonable, in Java, the class is static, unique, shared singleton, so use = = In the right place, the use of = = more to illustrate the implementation of the relationship.
So how is class<?>.isinstanceof (obj) implemented?
Objclass.getclass (). Isinstance (obj) is actually a query of objects of type-A, which illustrates the indirect implementation relationships of classes and objects. When a class inherits an interface, class, or abstract class, the isinstanceof is designed to find out what classes this object actually implements and how to compare the ports. Interface i{} class A implements I{}class B extends a{}b b = new B (); i.class.isinstance (b); Query B is not implemented interface I trueA.class.isInstance (b);//query B is not implemented a class trueB.class.inInstance (b);//query B is not implemented Class B True its implementation is a layer of query (Java source code using native codes, this is not written here, it is called by the following methods to query)//The stop position of the query is Object.classif (a==null) {return false;} if (A.getclass () ==i.class) {return true;} if (A.getclass (). Getsuperclass () ==i.class | | a.getclass (). Getinterfaces () ==i.class) {return true;} ...//If the query gets, return true;//otherwise terminate at Java.lang.Object.class
Comparison of operators instanceof and Class<?>.isinstance in Java