博文地址

我的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. angular6 监听url查询参数变化刷新页面

    快照snapshot取到的参数是组件第一次渲染时候的参数,当我们在页面中需要根据不同的url查询参数显示不同的内容时,快照就不能满足我们的需要了,这时候就要用ActivatedRoute服务的quer ...

  2. centos7.2下安装python3.6.5

    yum groupinstall 'Development Tools' 出现错误 系统 centos 7.3 使用命令 yum groupinstall 'Development Tools' 出现 ...

  3. 豆瓣读书isbn 查询

    最近学习微信小程序,做一个类似"书库"的小demo,大致流程使用摄像头获取书本后面的isbn,通过豆瓣读书API得到书本介绍.豆瓣评分.图书评论等信息,然鹅https://api. ...

  4. 机器学习——k-均值算法(聚类)

    文章目录 k-均值(k-means)聚类 1.k-均值算法 2.k-均值算法的代价函数 3.k-均值算法步骤 4.初始化聚类中心点和聚类个数 5.sklearn实现k-means算法 k-均值(k-m ...

  5. 遍历php的_SERVER数组键值信息

    $_SERVER 是一个包含了诸如头信息(header).路径(path).以及脚本位置(script locations)等等信息的数组.这个数组中的项目由 Web 服务器创建.不能保证每个服务器都 ...

  6. echars配置案例-reactnative

    option = { backgroundColor:'#fff', grid: { left: '3%', right: '4%', top:, bottom: '6%', containLabel ...

  7. 【oracle】去重

    基本去重: SELECT DISTINCT * FROM TABLE; 其他去重: 待添加

  8. 【java】String与Date转换

    String转Date            String date="";            SimpleDateFormat format=new SimpleDateFo ...

  9. 【myBatis】It's likely that neither a Result Type nor a Result Map was specified.

    因为mapper.xml里把resultType写成了parameterType

  10. Chrome DevTools的使用

    一.Chrome DevTools 简介 Chrome 开发者工具是一套内置于Google Chrome中的Web开发和调试工具,可用来对网站进行迭代.调试和分析 手册:Chrome 开发者工具中文手 ...