Java中instanceof和isInstance的具体区别
2018-03-29 11:03
253 查看
本文转载自:https://www.cnblogs.com/yueshangzuo/p/8549477.html在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考SOF上的一些回答如下:obj.instanceof(class)
表示对象obj是否是class类或其子类的对象一个对象是自身类的一个对象
一个对象是自身类父类和接口的一个对象
所有对象都是Object类的对象
凡是null有关的都是false
class.isInstance(obj)
文档中这样描述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一个对象能转化为自身类的对象
一个对象能被转化为自身类的父类和实现的接口的对象
所有对象都能转化为Object类的对象
凡是null有关的都是false
可见与instanceof用法相同,关键在于动态等价动态等价性
表示对象obj是否是class类或其子类的对象一个对象是自身类的一个对象
一个对象是自身类父类和接口的一个对象
所有对象都是Object类的对象
凡是null有关的都是false
class.isInstance(obj)
文档中这样描述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一个对象能转化为自身类的对象
一个对象能被转化为自身类的父类和实现的接口的对象
所有对象都能转化为Object类的对象
凡是null有关的都是false
可见与instanceof用法相同,关键在于动态等价动态等价性
表示对象obj是否是class类或其子类的对象一个对象是自身类的一个对象
一个对象是自身类父类和接口的一个对象
所有对象都是Object类的对象
凡是null有关的都是false
class.isInstance(obj)
文档中这样描述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一个对象能转化为自身类的对象
一个对象能被转化为自身类的父类和实现的接口的对象
所有对象都能转化为Object类的对象
凡是null有关的都是false
可见与instanceof用法相同,关键在于动态等价动态等价性
表示对象obj是否是class类或其子类的对象一个对象是自身类的一个对象
一个对象是自身类父类和接口的一个对象
所有对象都是Object类的对象
凡是null有关的都是false
class.isInstance(obj)
文档中这样描述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一个对象能转化为自身类的对象
一个对象能被转化为自身类的父类和实现的接口的对象
所有对象都能转化为Object类的对象
凡是null有关的都是false
可见与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.instanceof(class)
表示对象obj是否是class类或其子类的对象一个对象是自身类的一个对象
一个对象是自身类父类和接口的一个对象
所有对象都是Object类的对象
凡是null有关的都是false
class.isInstance(obj)
文档中这样描述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一个对象能转化为自身类的对象
一个对象能被转化为自身类的父类和实现的接口的对象
所有对象都能转化为Object类的对象
凡是null有关的都是false
可见与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 = f483 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.instanceof(class)
表示对象obj是否是class类或其子类的对象一个对象是自身类的一个对象
一个对象是自身类父类和接口的一个对象
所有对象都是Object类的对象
凡是null有关的都是false
class.isInstance(obj)
文档中这样描述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一个对象能转化为自身类的对象
一个对象能被转化为自身类的父类和实现的接口的对象
所有对象都能转化为Object类的对象
凡是null有关的都是false
可见与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),编译器在运行时才进行类型检查,故可用于反射,泛型中
相关文章推荐
- Java中instanceof和isInstance区别详解
- Java中instanceof和isInstance区别详解
- java 类型信息 instanceof 和 isInstance区别
- java开发中instanceOf 和isInstance的区别
- Java里isInstance,instanceof和isAssignableFrom的区别
- 详谈Java中instanceof和isInstance的区别
- java 类型信息 instanceof 和 isInstance区别
- 详解java和javascript的具体区别
- 从头认识java-12.3 instanceof、isInstance和isAssignableFrom
- java中public/protected/private的具体区别
- java final 和instanceof 关键字的区别
- java 接口、抽象类、具体类、内部类、匿名内部类的区别及它们之间的关系
- Java 类型信息 instanceof、isInstance和isAssignabelFrom
- java-----instanceof与getClass的区别
- java基础之 “==”和“ equals”以及instanceof的区别
- instanceof, isinstance,isAssignableFrom的区别
- instanceof, isinstance,isAssignableFrom的区别
- instanceof, isinstance,isAssignableFrom的区别
- instanceof和isInstance(Object obj) 和isAssignableFrom(Class cls)的区别和联系
- instanceof, isinstance,isAssignableFrom的区别