Java judges the relationship between classes and Instances

Source: Internet
Author: User

Java judges the relationship between classes and Instances

?? We usually use the instanceOf keyword to determine whether an object is a class instance. Recently, the blogger saw the isInstance keyword, which is not clear about the difference with instanceOf. Therefore, du Niang's posture rose by the way.
?? In Java, there are three keywords for determining the relationship between classes and instances: instanceof, isInstance, and isAssignableFrom.

Instanceof: instance isInstance used to determine whether the object is a class: used to determine whether the object belongs to an instance of a certain type isAssignableFrom: used to determine whether there is a derivative relationship between types

?? After reading the above explanation, is it still in the fog?
?? The following is a detailed description.
?? The instanceof operator is only used to reference variables of objects. Check whether the tested object on the left is instantiated by the right class or interface. If the tested object is null, the test result is always false.
?? The image is described as follows: instanceof its own instance or subclass instance returns true
Example:

String s=new String("javaisland");System.out.println(s instanceof String); //true

?? The isInstance (Object obj) method of the Class class. obj is the tested Object. If obj is an instance of the Class or interface that calls this method, true is returned. This method is the dynamic equivalent of the instanceof operator.
?? Image Description: its class. class. isInstance (its own instance or subclass instance) returns true
Example:

String s=new String("javaisland");System.out.println(String.class.isInstance(s)); //true

?? The isAssignableFrom (Class cls) method of the Class class. If the Class or interface that calls this method is the same as the Class or interface represented by the parameter cls, or the parent class of the Class or interface represented by the parameter cls, returns true.
?? Image Description: its class. class. isAssignableFrom (its class or subclass. class) returns true
Example:

System.out.println(ArrayList.class.isAssignableFrom(Object.class));  //falseSystem.out.println(Object.class.isAssignableFrom(ArrayList.class));  //true

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.