Java---深刻理解instanceof(绝对让你有新发现)

1、前言
  • 1.一、instanceof 是 Java 的保留关键字,一个二元操做符(和==,>,<是同一类东东)
  • 1.二、做用:测试它左边的对象是不是它右边的类的实例
  • 1.三、返回值:返回 boolean 的数据类型。
  • 接下来经过本文给你们介绍Java 中的instanceof用法详解及instanceof是什么意思,须要的朋友参考下吧
2、instanceof 的说明、解释
  • 2.一、说明:测试

    •  (1).一个类的实例包括自己的实例, 以及全部直接或间接子类的实例
    • (2).instanceof左边操做元显式声明的类型与右边操做元必须是同种类或有继承关系, 即位于继承树的同一个分支上, 不然会编译出错 !
  • 2.二、code

    • 2.2.1_demo01_对象

      double obj = 1;
      if (obj instanceof Double)
          System.out.println("true");      //无输出-->不成立
      • 编译信息: Incompatible conditional operand types double and Double错误
      • 分析: obj 必须是对象的实例。不能是基础数据类型。
    • 2.2.2_demo02_继承

      String obj = 1.0 + "";
      if (obj instanceof Double)
          System.out.println("true");    //无输出:
      • 编译信息:Incompatible conditional operand types String and Double" 错误
      • 分析:String 和 Double 不是位于继承树的同一个分支上。 
    • 2.2.3_demo03_token

      if(null instanceof Object)
          System.out.println("true");    
      else
          System.out.println("false");
      //输出:false
      String obj = null;
      
      if (obj instanceof Object){
          System.out.println("true");
      else
          System.out.println("false");
      //输出:false
      • 分析:null用操做符instanceof测试任何类型时都是返回false的
    • 2.2.4_demo04_接口

      String obj = null;
      if (obj instanceof null)
          System.out.println("true");
      else
          System.out.println("false");
      • 编译信息:Syntax error on token "null", invalid ReferenceType" 标记"null"上的语法错误,无效引用类型
    • 2.2.5_demo05_:前方高能,务必仔细阅读!!!编译器

      class Student {}
       public class Test {
           public static void main(String[] args){
               //第1组
               System.out.println(new Student() instanceof String);    //编译时错误
               System.out.println(new Student() instanceof Exception); //编译时错误
               System.out.println(new Student() instanceof Object);   //输出时true
               //第2组
               System.out.println(new Student() instanceof List);    //输出false
               System.out.println(new Student() instanceof List<?>); //输出false
               System.out.println(new Student() instanceof List<String>); //编译错误
               System.out.println(new Student() instanceof List<Object>); //编译错误
               //第3组
               System.out.println(new String() instanceof List);    //编译错误
               System.out.println(new String() instanceof List<?>); //编译错误
               //第4组
               System.out.println(null instanceof Object);      //输出错误,同demo03
          }
      }
      • 分析:it

        • 对于第1组:与下面一块儿论述
        • 对于第2组:
        • 疑惑1:为何用student()测试String时编译报错,测试List的时编译经过io

          • 猜想:(1)难道是instanceof对接口在编译时不做检查呢,(2)仍是因为List类型自己在编译时不肯定具体类型致使的
          • 推翻猜想:与第3组用 String对象测试List<>和List<?>时结果大不相同
          • 论据:翻看Java书籍时发现:instanceof 的这些表现都是跟cast操做符是有点关系的,就是说当咱们在instanceof方法时,编译器会检查这个对象可以cast到右边的类型。若是不能cast则直接报错,若是不能肯定类型,则经过编译,具体看运行时定。
        • 疑惑2:Student已经肯定类型了啊,List类型也是肯定的啊,为何可以编译经过呢,而String却不行呢?``编译

          • 猜想:难道是本身定义的类和系统定义的类在编译处理上有不一样?
          • 推论:多是由于final关键字的区别形成的,咱们发现不论String、Integer、Long等都是最终类,他们的处理相似于编译器对常量的处理,故而能够经过编译
          • 论据:其实当咱们在自定义的类前面加上final关键字的时候,其表现就跟String、Integer、Long这些最终类测试instanceof表现的同样了。
3、instanceof 的用法
  • 用法:result = object instanceof class
  • 参数:

    • result:布尔类型值
    • object:类的对象
    • class: 对象类
  • 说明:若是 object 是 class 的一个实例,则 instanceof 运算符返回 true。若是 object 不是指定类的一个实例,或者 object 是 null,则返回 false。
4、综述
  • 相信任何一个认真读完以上内容的人都会对instanceof的用法、理解更加深入!
  • 但愿做者的文章能够被您采纳!您的支持是做者坚持的无限动力!Thanks!
相关文章
相关标签/搜索