Instanceof and isinstance differences in Java

Source: Internet
Author: User

A one-time fix instanceof and isinstance,instanceof and isinstance are very similar, and the usage is similarly, first look at these two usages:

Obj.instanceof (Class)

Which means that this object is not of this type,

1. An object is an object of its own class

2. An object is an object of its own class parent class (parent of the parent Class) and interface (interface of the interface)

3. All objects are object

4. All null-related is False null.instanceof (class)

Class.ininstance (obj)

Can this object be converted into this class?

1. An object is an object of its own class

2. An object can be converted to an inherited class of its own class (the parent class of the parent class, etc.) and the implemented interface (the parent interface of the interface) strongly turns

3. All objects can be strongly transferred by the object

4. All null-related is false class.ininstance (NULL)

Class name. class and object. GetClass () is almost indistinguishable, because a class is the only class that is loaded by the ClassLoader.

An example is done:

classA {}classBextendsA {} Public classTest { Public Static voidMain (string[] args) {b b=NewB (); A A=NewA (); A BA=NewB (); System.out.println ("1------------"); System.out.println (binstanceofB); System.out.println (binstanceofA); System.out.println (binstanceofObject); System.out.println (NULL instanceofObject); System.out.println ("2------------");        System.out.println (B.getclass (). Isinstance (b));        System.out.println (B.getclass (). Isinstance (a)); System.out.println ("3------------");        System.out.println (A.getclass (). Isinstance (BA));        System.out.println (B.getclass (). Isinstance (BA)); System.out.println (B.getclass (). Isinstance (NULL)); System.out.println ("4------------"); System.out.println (A.class. Isinstance (a)); System.out.println (A.class. Isinstance (b)); System.out.println (A.class. Isinstance (BA)); System.out.println ("5------------"); System.out.println (B.class. Isinstance (a)); System.out.println (B.class. Isinstance (b)); System.out.println (B.class. Isinstance (BA)); System.out.println ("6------------"); System.out.println (Object.class. Isinstance (b)); System.out.println (Object.class. Isinstance (NULL)); }}

Operation Result:

Instanceof and isinstance differences 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.