isAssignableFrom
‘Class.isAssignableFrom’方法是用来判断一个类Class1和另外一个类Class2是否相同或Class1是Class2的超类或接口。
一般调用格式是:
Class1.isAssignableFrom (Class2)
调用者和参数都是java.lang.Class类型。java
instanceof
而 instanceof 运算符是用来判断一个对象实例是不是一个类或接口的实例。
格式是:obj instanceof TypeName
第一个参数是对象实例名,第二个参数是具体的类名或接口名。
Class.isInstance(Object obj) 与 instanceof等价。ide
Java里,引用的类型是静态肯定的——源码里声明是什么类型就是什么类型;而对象的实际类型是动态肯定的。
instanceof 运算符它的运行时语义并不关心它的左操做数的引用的静态类型,而只关心它所引用的对象的实际类型是否跟右操做数所指定的类型相匹配。this
instanceof 是如何进行判断的?.net
At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.对象
也就是说,对于 obj instanceof TypeName
若是 obj 能够转换为 TypeName 类型(没有抛出ClassCastException异常)则返回 true,不然返回 false。接口
区别
instanceof是Java语言的语法结构,而该语法要求 instanceof 运算符的右操做数是一个引用类型名,也就意味着右操做数必须是一个编译时的常量
isInstance是 Java 标准库里的一个方法,其被调用对象(receiver object,也就是“this”)等价于 instanceof 运算符的右操做数的意义,但能够是运行时决定的任意 java.lang.Class 对象,而不要求是编译时常量。这就比 instanceof 运算符要灵活一些。
import org.junit.Test;get
class Person {
private String name;
private int age;源码
public Person() {
name = "lgh";
age = 25;
}it
public Person(String name, int age) {
this.name = name;
this.age = age;
}io
public Person(int age, String name) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class T {
@Test
public void test() {
Person person1 = new Person("111", 24);
Person person2 = new Person("222", 25);
System.out.println(person1.getClass().isAssignableFrom(person2.getClass()));//true
System.out.println(Object.class.isAssignableFrom(person1.getClass()));//true
System.out.println(person1.getClass().isAssignableFrom(Object.class));//false
System.out.println(person1 instanceof Person);//true System.out.println(person1 instanceof Object);//true } }