您的位置:首页 > 编程语言 > Java开发

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用法相同,关键在于动态等价动态等价性
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),编译器在运行时才进行类型检查,故可用于反射,泛型中
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinking in java