isAssignableFrom
Example: Class1.isAssignableFrom(Class2)
Explanation: ① Determine whether Class1 and Class2 are the same.
System.out.println(Object.class.isAssignableFrom(Object.class)); // true
② Whether Class1 is the parent class or interface of Class2.
System.out.println(Object.class.isAssignableFrom(String.class)); // true
System.out.println("Object类是String 类的父类:"+
Object.class.isAssignableFrom(String.class));//true
System.out.println("AbstractList类是ArrayList 类的父类:"+
AbstractList.class.isAssignableFrom(new ArrayList<>().getClass()));//true
System.out.println("List接口是ArrayList 类的父类:"+
List.class.isAssignableFrom(new ArrayList<>().getClass()));//true
System.out.println("List接口是AbstractList 接口(抽象类)的父接口:"+
List.class.isAssignableFrom(AbstractList.class));//true
According to the relationship between ArrayList, AbstractList and List, isAssignableFrom can also judge whether class1 is the parent interface (abstract class) of class2
instanceof
example: object instanceof TypeName
Explanation: Determine whether an object instance is an instance of a class or interface or its subclass subinterface.
public void testInstanceOf1() {
String ss = "";
System.out.println(ss instanceof Object); // true
System.out.println(ss instanceof java.lang.String); // true
ArrayList<String>array=new ArrayList<>();
System.out.println(array instanceof java.util.List); // true
}
The above is recorded as a daily note.over