Class. isassignablefrom (class clz) method and instanceof

Source: Internet
Author: User

Isassignablefrom is used to determine whether a class class1 is the same as another class class2 or whether it is a superclass or interface of another class.
The call format is
Class1.isassignablefrom (class2)
Callers and parameters are of the Java. Lang. class type.

This method returns true in the following cases
1. If class1 and class2 are of the same type, true is returned.
2. If class1 is a class2 superclass or superinterface, true is returned.

For example, if the object. Class. isassignablefrom (string. Class) object is a string superclass, true is returned.
String. Class. isassignablefrom (string. Class) returns true.


Instanceof is an instance used to determine whether an object instance is a class or interface or its sub-interfaces.
Format: OO instanceof typename
The first parameter is the object instance name, and the second parameter is the specific class name or interface name.

An example is as follows:

package test;             public class Test2 {             public  void  testIsAssignedFrom1() {                 System.out.println(  String.class.isAssignableFrom(Object.class) ) ;             }                         public  void  testIsAssignedFrom2() {                 System.out.println(  Object.class.isAssignableFrom(Object.class) );             }                         public  void  testIsAssignedFrom3() {                 System.out.println( Object.class.isAssignableFrom(String.class) );             }                         public  void  testInstanceOf1() {                 String  ss  =  "";                 System.out.println(  ss  instanceof Object );             }                         public  void  testInstanceOf2() {                 Object  o = new   Object();                 System.out.println(  o  instanceof  Object );             }                                public static void main(String[] args){              Test2 test = new Test2();              test.testIsAssignedFrom1();              test.testIsAssignedFrom2();              test.testIsAssignedFrom3();              test.testInstanceOf1();              test.testInstanceOf2();          }    }    

Result:

False

True

True

True

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.