Java中instanceof和isInstance的具体区别

本文转载自:https://www.cnblogs.com/yueshangzuo/p/8549477.html

在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考SOF上的一些回答如下:

  • obj.instanceof(class)

表示对象obj是否是class类或其子类的对象

  1. 一个对象是自身类的一个对象
  2. 一个对象是自身类父类和接口的一个对象
  3. 所有对象都是Object类的对象
  4. 凡是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

  1. 一个对象能转化为自身类的对象
  2. 一个对象能被转化为自身类的父类和实现的接口的对象
  3. 所有对象都能转化为Object类的对象
  4. 凡是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类或其子类的对象

  1. 一个对象是自身类的一个对象
  2. 一个对象是自身类父类和接口的一个对象
  3. 所有对象都是Object类的对象
  4. 凡是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

  1. 一个对象能转化为自身类的对象
  2. 一个对象能被转化为自身类的父类和实现的接口的对象
  3. 所有对象都能转化为Object类的对象
  4. 凡是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类或其子类的对象

  1. 一个对象是自身类的一个对象
  2. 一个对象是自身类父类和接口的一个对象
  3. 所有对象都是Object类的对象
  4. 凡是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

  1. 一个对象能转化为自身类的对象
  2. 一个对象能被转化为自身类的父类和实现的接口的对象
  3. 所有对象都能转化为Object类的对象
  4. 凡是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),编译器在运行时才进行类型检查,故可用于反射,泛型中

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值