The difference between the Class.isassignablefrom (Class Clz) method and the Instanceof keyword

Source: Internet
Author: User

Class.isassignablefrom () is a subclass or interface that is used to determine whether a class Class1 and another class Class2 are the same or another class.
The format is:
Class1.isassignablefrom (CLASS2)
Both the caller and the parameter are java.lang.Class types.
andinstanceofis used to determine whether an object instance is an instance of a class or interface or its subclass sub-interface.
The format is: o instanceof TypeName
   The first parameter is the object instance name, and the second parameter is the specific class name or interface name, for example,   String,inputstream. Java code    package com.bill99.pattern;      public class  assignabletest {              public  Assignabletest (string name)  {       }        /**       *  Determine whether a class is a parent of another class        *  is print true       *  no print false       */       public static void testisassignedfrom1 ()  {            system.out.println ("String is the parent class of object:" +string.class.isassignablefrom ( Object.class);       }       /**        *  determine whether a class is a parent class of another class        *  is print true       *  no print false        */       public static void testisassignedfrom2 ()  {            system.out.println ("object is a string's parent class:" + Object.class.isAssignableFrom (String.class));       }        /**       *  Determine if a class is the same as another class        *   Print true       *  no print false       */        PUBLIC STATIC VOID TESTISASSIGNEDFROM3 ()  {            system.out.println ("Object and object are the same:" +object.class.isassignablefrom ( Object.class);       }          /**       *  determine if STR is an instance of object class        *  is print true        *  no print false       */        Public static void testinstanceof1 ()  {            string str = new string ();            system.out.print ("Str is an instance of object:");            System.out.println (str instanceof object);       }        /**       *  judgment O is an instance of object class         *  print true       *  no print false       */        public static void testinstanceof2 ()  {      &nbsP;    object o = new object ();            system.out.print ("O is an instance of object:");            system.out.println (o instanceof object);       }               public static void main ( String[] args)  {           testisassignedfrom1 ();            testisassignedfrom2 ();            TESTISASSIGNEDFROM3 ();            testinstanceof1 ();           testinstanceof2 ();        }  }  

Results:

String is the parent class of object: False
object is the parent class of string: True
object is the same as object: True
STR is an instance of object: True
O is an instance of object: 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.