博文地址

我的GitHub 我的博客 我的微信 我的邮箱
baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

目录

简介

区别:

  • instanceof 是一个运算符,用来判断前面的对象是不是后面指定的类型
  • isInstance 是 Class 类的一个方法,用来判断指定的对象是否可以强转为当前类型

Class.isInstance 官方文档描述:

  • 判定指定的 Object 是否与此 Class 所表示的对象赋值兼容
  • 此方法是 Java 语言 instanceof 运算符的动态等效方法
  • 如果指定的 Object 参数非空,且能够在不引发 ClassCastException 的情况下被强制转换成该 Class 对象所表示的引用类型,则该方法返回 true;否则返回 false。
  • 特别地:
    • 当该 Class 对象表示一个已声明的时,若指定的 Object 参数是所表示类(或其任一子类)的一个实例,则此方法返回 true;否则返回 false
    • 如果此 Class 对象表示一个数组类,且通过身份转换或扩展引用转换,指定的 Object 参数能转换为一个数组类的对象,则返回 true;否则返回 false
    • 如果此 Class 对象表示一个接口,且指定 Object 参数的类或任一超类实现了此接口,则此方法返回 true;否则返回 false
    • 如果此 Class 对象表示一个基本类型,则此方法返回 false

最重要的一句话:凡是 null 相关的都是 false

测试

继承关系

interface IPeron {
} class Father implements IPeron {
} class Child extends Father {
}

测试案例

基本测试

System.out.println("------------基本测试------------");
Child child = new Child(); //只要能强转就为 true;反过来,只要为 true,我们就能安全的强转
System.out.println(child instanceof Child && child instanceof Father && child instanceof IPeron && child instanceof Object);//true
System.out.println(Child.class.isInstance(child) && Father.class.isInstance(child) && IPeron.class.isInstance(child) && Object.class.isInstance(child));//true

测试多态

System.out.println("------------测试多态------------");
IPeron peron = new Child();
System.out.println(peron instanceof Child && peron instanceof Father && peron instanceof IPeron && peron instanceof Object);//true

测试null

System.out.println("------------测试null------------");
Father father = null; //凡是null相关的都是false
System.out.println(null instanceof Object || Object.class.isInstance(null) || father instanceof Object || Object.class.isInstance(father)); //false
Child nullChild = (Child) father; //这样是没问题的
//String nullString = (String) father; //编译不通过。Error:(39, 38) java: 不兼容的类型: Father无法转换为java.lang.String

测试数组

System.out.println("------------测试数组------------");
Child[] children = new Child[]{};
System.out.println(children instanceof Child[] && children instanceof Father[] && children instanceof IPeron[] && children instanceof Object[]);//true
System.out.println(Child[].class.isInstance(children) && Father[].class.isInstance(children) && IPeron[].class.isInstance(children) && Object[].class.isInstance(children));//true
System.out.println(new Father[]{} instanceof Child[]); //false

测试集合

System.out.println("------------测试集合------------");
List<Child> childList = new ArrayList<>();
System.out.println(childList instanceof List && childList instanceof ArrayList && List.class.isInstance(childList) && ArrayList.class.isInstance(childList));//true

测试基本类型

System.out.println("------------测试基本类型------------");
int i = 0;
Integer integer = 0;
//System.out.println(i instanceof Object);//编译不通过
System.out.println(integer instanceof Integer && integer instanceof Object);//true
System.out.println(Integer.class.isInstance(i) && Object.class.isInstance(i) && Integer.class.isInstance(integer) && Object.class.isInstance(integer)); // true
System.out.println(int.class.isInstance(i) || int.class.isInstance(integer)); // false。如果此 Class 对象表示一个基本类型,则此方法返回 false

打印结果

------------基本测试------------
true
true
------------测试多态------------
true
------------测试null------------
false
------------测试数组------------
true
true
false
------------测试集合------------
true
------------测试基本类型------------
true
true
false

2019-11-17

- instanceof 和 isInstance 强转 类型 class MD的更多相关文章

  1. Java中instanceof和isInstance区别详解

     一次性搞定instanceof和isInstance,instanceof和isInstance长的非常像,用法也很类似,先看看这两个的用法: obj.instanceof(class) 也就是说这 ...

  2. java instanceof和isInstance的关系 精析

      1.instanceof 用途:判断这个object对象是不是这种Class类型. 语法: boolean result = object instanceof Class; 用法: 判断obje ...

  3. instanceof和isInstance的区别

    instanceof 是一个操作符(类似new, ==等) ( Object reference variable ) instanceof (class/interface type) if(a i ...

  4. Java之"instanceof"和"isInstance"代码举例

    源码: /** * @Date:2018-04-20 * @Description:判断Instance * - instanceof方法返回一个boolean类型的值,意在告诉我们对象是不是某个特定 ...

  5. Java中instanceof和isInstance的具体区别

    Java中instanceof和isInstance的具体区别 在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考 ...

  6. instanceof 和isinstance的区别

    class A {} class B extends A {} class C extends A {} public class Test { public static void main(Str ...

  7. Python中为什么推荐使用isinstance来进行类型判断?而不是type

    转自:http://www.xinxingzhao.com/blog/2016/05/23/python-type-vs-isinstance.html Python在定义变量的时候不用指明具体的的类 ...

  8. C++枚举类型Enum及C++11强枚举类型用法

    C++中的枚举类型常常和switch配合使用,这里用一个简单的switch控制键盘回调的代码片段来说明枚举的用法: //W A S D 前.后.左.右行走 enum Keydown{ Forward= ...

  9. C++11 强枚举类型

    在标准C++11之前的枚举是继承C的,枚举类型不是类型安全的.枚举类型被视为整数,这使得两种不同的枚举类型之间可以进行比较. 一.C中enum类型的局限语法: enum type1{a, b, c}; ...

随机推荐

  1. 4 Linux文件与目录管理

    Linux的目录文件结构为树状结构,最顶级目录为根目录 / 其他目录通过挂载可以将他们添加到树中,通过解除挂载可以移除他们 绝对路径: 根目录开始 eg: /usr/share/doc 相对路径: 不 ...

  2. 【spring data jpa】带有条件的查询后分页和不带条件查询后分页实现

    一.不带有动态条件的查询 分页的实现 实例代码: controller:返回的是Page<>对象 @Controller @RequestMapping(value = "/eg ...

  3. nginx之旅(第一篇):nginx下载安装、nginx启动与关闭、nginx配置文件详解、nginx默认网站

    一.nginx下载安装 版本nginx 1.15.5 系统环境centos7.5(本机ip192.168.199.228) 关闭selinux 和防火墙firewall 1.下载 wget http: ...

  4. itextpdf5操作文本

    itextpdf使用document操作文本可以使用3个对象来做:Chunk.Phrase.Paragraph. itextpdf5的包对它们的介绍是这样的: chunk: 这是可以添加到文档中最小的 ...

  5. 分布式系统原理之cap理论

    1.1 CAP理论的含义 Cap理论表示在分布式系统中一致性(C).可用性(A)和分区容错性(P)最多只能同时满足两个.  一致性:客户端更新数据成功后,在任意时刻,在系统任意对外提供服务的节点,读取 ...

  6. 分布式异步框架celery

    Celery 1.什么是Clelery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 Celery架构 Celery的架构由三部分组 ...

  7. MySQL数据物理备份之xtrabackup

    percona-xtrabackup 它是开源免费的支持MySQL 数据库热备份的软件,它能对InnoDB和XtraDB存储引擎的数据库非阻塞地备份.它不暂停服务创建Innodb热备份: 为mysql ...

  8. 【视频技术】FFMPEG应用(转)

    https://blog.csdn.net/kaikai136412162/article/details/80746961 https://blog.csdn.net/weixin_37897683 ...

  9. Codeforces 379F New Year Tree

    F. New Year Tree time limit per test2 seconds memory limit per test256 megabytes You are a programme ...

  10. [RN] React Native 使用 图片预览和放大 插件 react-native-image-zoom-viewer 过程中,放大报错问题

    React Native 使用 图片预览和放大 插件 react-native-image-zoom-viewer 过程中,放大报错问题 报错如下: Cannot record touch end w ...