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