博文地址

我的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. ta和夏天一起来了

    目录 ta和夏天一起来了 上半年,过去的就让去过去,遗憾的也别再遗憾. 下半年,拥有的请好好珍惜,想要的请努力去追. ta和夏天一起来了 ​ 转眼结束了2019的上半年,在这个月末, 季度末, 周末, ...

  2. httpclient解析

    1.HttpClient简介 HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性,它不仅使客户端发送Http请求变得容易,而且也方便开发人员测试接口(基于Http ...

  3. Tomcat9在CentOS7上启动慢解决办法,实测可行

    1.修改jdk目录下的java.security文件 vim /usr/lib/jvm/jdk1.8.0/jre/lib/security/java.security 将如下配置securerando ...

  4. ARM开发板上查看动态库或者可执行程序的依赖关系

    以ARM32开发板为例,在/lib下有一个名为ld-linux-armhf.so.3的可执行程序(在ARM64开发板上是/lib/ld-linux-aarch64.so.1),这个程序负责加载可执行程 ...

  5. 重新编译kubeadm,修改默认证书时间

    参考 kubeadm alpha certs renew Kubeadm1.14 证书调整 kubeadm 部署的 kubernetes 集群,默认的证书有效时间是1年,需要每年手工更新. 1. 重新 ...

  6. css之单行缩略..以及多行缩略

    html单行缩略方法 .oneline { white-space: nowrap; //强制文本在一行内输出 overflow: hidden; //隐藏溢出部分 text-overflow: el ...

  7. Spring-Data-Redis 入门学习

    Spring-Data-Redis 入门学习 参考文章: https://www.jianshu.com/p/4a9c2fec1079 导入 redis 相关依赖 <dependency> ...

  8. stm32的flash操作注意事项

    从STM32编程手册中,可以知道:在进行写或擦除操作时,不能进行代码或数据的读取操作. 比如:你在写Flash期间有接收串口数据,很有可能会丢串口数据. 因为比较耗时,所以,在写数据时,CPU不会执行 ...

  9. 原生ajax分页,无刷新分页,最简化。超简单,代码最少

    <html><script> var page=1; // 页面第一次加载,显示第一页 window.onload=function(){ ajax_go(1) } //分页的 ...

  10. getpeername、getsockname

    函数原型: #include<sys/socket.h> int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t ...