Java isAssignableFrom () method and instanceof () method usage, javainstanceof usage
Summary:
- IsAssignableFrom ()The method is fromClass inheritanceFrom the perspective,Instanceof ()The method is fromInstance inheritance.
- IsAssignableFrom ()The method is to determine whether it is the parent class of a class,Instanceof ()The method is to determine whether a class is a subclass.
1. Class. isAssignableFrom () method
Class. isAssignableFrom ()Is used to determine whether a class Class1 is the same as another class Class2 or a subclass or interface of another class.
Format:
Class1.isAssignableFrom (Class2)
Callers and parameters are of the java. lang. Class type.
2. Class. instanceof () method
Class. instanceof ()Is an instance used to determine whether an object instance is a class or interface or its sub-interfaces.
Format:
Obj instanceof TypeName
The first parameter is the object instance name, and the second parameter is the specific class name or interface name, such as String and InputStream. The return value is boolean.
3. Usage