本文转载自:https://www.cnblogs.com/yueshangzuo/p/8549477.html
在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考SOF上的一些回答如下:
表示对象obj是否是class类或其子类的对象
文档中这样描述
Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator.
即对象obj能否转化为类class的对象,动态等价于instanceof
可见与instanceof用法相同,关键在于动态等价
class Father{}
class Son extends Father{}
public class Test{
public static boolean DynamicEqual(Object fatherObj,Object sonObj){
return fatherObj.getClass().isInstance(sonObj); // pass
// return sonObj instanceof Father; // pass
// return sonObj instanceof (fatherObj.getClass()); //error
}
public static void main(String[] args){
//same using
Father father = new Father();
Son son = new Son();
System.out.println(son instanceof Son); // true
System.out.println(son instanceof Father); // true
System.out.println(son instanceof Object); // true
System.out.println(null instanceof Object); // false
System.out.println();
System.out.println(Son.class.isInstance(son)); // true
System.out.println(Father.class.isInstance(son)); // true
System.out.println(Object.class.isInstance(son)); // true
System.out.println(Object.class.isInstance(null)); // false
System.out.println();
//different using
System.out.println(DynamicEqual(father, son));
}
}
对obj.instanceof(class),在编译时编译器需要知道类的具体类型
对class.isInstance(obj),编译器在运行时才进行类型检查,故可用于反射,泛型中
在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考SOF上的一些回答如下:
表示对象obj是否是class类或其子类的对象
文档中这样描述
Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator.
即对象obj能否转化为类class的对象,动态等价于instanceof
可见与instanceof用法相同,关键在于动态等价
class Father{}
class Son extends Father{}
public class Test{
public static boolean DynamicEqual(Object fatherObj,Object sonObj){
return fatherObj.getClass().isInstance(sonObj); // pass
// return sonObj instanceof Father; // pass
// return sonObj instanceof (fatherObj.getClass()); //error
}
public static void main(String[] args){
//same using
Father father = new Father();
Son son = new Son();
System.out.println(son instanceof Son); // true
System.out.println(son instanceof Father); // true
System.out.println(son instanceof Object); // true
System.out.println(null instanceof Object); // false
System.out.println();
System.out.println(Son.class.isInstance(son)); // true
System.out.println(Father.class.isInstance(son)); // true
System.out.println(Object.class.isInstance(son)); // true
System.out.println(Object.class.isInstance(null)); // false
System.out.println();
//different using
System.out.println(DynamicEqual(father, son));
}
}
对obj.instanceof(class),在编译时编译器需要知道类的具体类型
对class.isInstance(obj),编译器在运行时才进行类型检查,故可用于反射,泛型中
在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考SOF上的一些回答如下:
表示对象obj是否是class类或其子类的对象
文档中这样描述
Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator.
即对象obj能否转化为类class的对象,动态等价于instanceof
可见与instanceof用法相同,关键在于动态等价
class Father{}
class Son extends Father{}
public class Test{
public static boolean DynamicEqual(Object fatherObj,Object sonObj){
return fatherObj.getClass().isInstance(sonObj); // pass
// return sonObj instanceof Father; // pass
// return sonObj instanceof (fatherObj.getClass()); //error
}
public static void main(String[] args){
//same using
Father father = new Father();
Son son = new Son();
System.out.println(son instanceof Son); // true
System.out.println(son instanceof Father); // true
System.out.println(son instanceof Object); // true
System.out.println(null instanceof Object); // false
System.out.println();
System.out.println(Son.class.isInstance(son)); // true
System.out.println(Father.class.isInstance(son)); // true
System.out.println(Object.class.isInstance(son)); // true
System.out.println(Object.class.isInstance(null)); // false
System.out.println();
//different using
System.out.println(DynamicEqual(father, son));
}
}
对obj.instanceof(class),在编译时编译器需要知道类的具体类型
对class.isInstance(obj),编译器在运行时才进行类型检查,故可用于反射,泛型中