End

instanceof 和 isInstance 强转 类型 class

本文地址


目录

instanceof 和 isInstance 强转 类型 class

简介

区别:

  • 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(注意:为null时,能强转,但为false);只要为 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

posted @   白乾涛  阅读(801)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2018-11-17 Intent 简介 结构 传递数据 常见Action 常量
2018-11-17 Intent 常用场景 FileProvider 拍照 裁剪
2016-11-17 RxJava 设计理念 观察者模式 Observable lambdas
点击右上角即可分享
微信分享提示