博文地址

我的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. group by 两个字段

    group by 的简单说明:  group by 一般和聚合函数一起使用才有意义,比如 count sum avg等 使用group by的两个要素:   (1) 出现在select后面的字段 要么 ...

  2. ECharts将折线变平滑和去掉点的属性(转载)曲线变圆滑

    本文链接:https://blog.csdn.net/sinat_36422236/article/details/62430114 series : [ { name:'your name', sy ...

  3. springboot搭建dubbo+zookeeper简单案例

    背景:只是自己使用单机版zookeeper搭建dubbo的一个学习案例,记录成功的过程 1.搭建zookeeper坏境 使用docker来构建环境 1.1 拉取镜像:docker pull zooke ...

  4. Spring的感知能力 Aware

    在 Spring 框架中有一个 org.springframework.beans.factory.Aware 接口, Aware 是感知感应的意思,那么此接口的作用就是为 Spring 中的 bea ...

  5. 201871010135-张玉晶《面向对象程序设计(Java)》第四周学习总结

    201871010135-张玉晶<面向对象程序设计(Java)>第四周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这 ...

  6. 201871010135 张玉晶 《面向对象程序设计(java)》第二周学习总结

    201871010135 张玉晶 <面向对象程序设计(java)>第二周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...

  7. ActiveMQ消息可靠性-事物

    事物偏生产者,签收偏消费者 设置为true,需要手动提交    设置为false,自动提交   使用手动提交的好处就是可以回滚,当整个事物提交时,里面的某条失败了,可以事物回滚,于是保证了数据的一致性 ...

  8. Linux磁盘管理——分区和文件系统

    1.分区类型 (1)主分区:最多只能有四个(主分区加扩展分区一共有四个). (2)扩展分区:最多有一个,是主分区中的其中一个.不能存储数据也不能格式化,必须再分成逻辑分区才能使用. (3)逻辑分区:是 ...

  9. windows下 zabbix agent心跳数据获取异常

    模板中的心跳监控项默认是主动性的,在windows下直接装上客户端后,如果不协调时间,可能会出现心跳数据异常, 因为是主动式的监控,agent上的数据主动的推送到server上,但是从server上看 ...

  10. cookie session jwt-token

    http是无状态的,即请求之间是相互独立的:即提供用户名/密码验证后,下次还需要再次提供 而cookie就是解决这个问题的 cookies 服务器验证通过后,在响应头中设置set-cookies,浏览 ...