Comparison of operators instanceof and Class<?>.isinstance in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.