instanceof, the difference between isinstance,isassignablefrom

Source: Internet
Author: User

instanceof operator: Used only for object reference variables, check that the test object on the left is not an instantiation of the right class or interface. If the object being measured is a null value, the test result is always false.

Image: Self instance or subclass instance Instanceof itself class returns True
Example: String S=new string ("Javaisland");
System.out.println (S instanceof String);//true

the class Isinstance (Object obj) method, where obj is the object being tested and returns true if Obj is an instance of the class or interface that called the method. This method is the dynamic equivalent of the instanceof operator, and if obj is null, the test result is always false. Image: Own class. Class.isinstance (instance of itself or subclass) returns True
Example: String S=new string ("Javaisland");
System.out.println (String.class.isInstance (s));//true

Class IsAssignableFrom (Class CLS) method, which returns true if the class or interface calling this method is the same as the classes or interfaces represented by the parameter CLS, or is the parent class of the class or interface that the parameter CLS represents.
Image: Own class. Class.isassignablefrom (Own class or subclass. Class) returns True
Example: System.out.println (ArrayList.class.isAssignableFrom (Object.class)); False
System.out.println (Object.class.isAssignableFrom (Arraylist.class)); True

Instanceof, Isinstance,isassignablefrom differences

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.