当前位置: 移动技术网 > IT编程>开发语言>Java > Java关键字instanceof用法及实现策略

Java关键字instanceof用法及实现策略

2020年08月05日  | 移动技术网IT编程  | 我要评论

  instanceof 严格来说是java中的一个双目运算符,用来测试一个对象是否为一个类的实例,用法为:

boolean result = obj instanceof class

  其中 obj 为一个对象,class 表示一个类或者一个接口,当 obj 为 class 的对象,或者是其直接或间接子类,或者是其接口的实现类,结果result 都返回 true,否则返回false。

  注意:编译器会检查 obj 是否能转换成右边的class类型,如果不能转换则直接报错,如果不能确定类型,则通过编译,具体看运行时定。

1、obj 必须为引用类型,不能是基本类型

int i = 0;
system.out.println(i instanceof integer);//编译不通过
system.out.println(i instanceof object);//编译不通过

  instanceof 运算符只能用作对象的判断。

2、obj 为 null

system.out.println(null instanceof object);//false

  关于 null 类型的描述在官方文档: 有一些介绍。一般我们知道java分为两种数据类型,一种是基本数据类型,有八个分别是 byte short int long float double char boolean,一种是引用类型,包括类,接口,数组等等。而java中还有一种特殊的 null 类型,该类型没有名字,所以不可能声明为 null 类型的变量或者转换为 null 类型,null 引用是 null 类型表达式唯一可能的值,null 引用也可以转换为任意引用类型。我们不需要对 null 类型有多深刻的了解,我们只需要知道 null 是可以成为任意引用类型的特殊符号。

  在javase规范中对 instanceof 运算符的规定就是:如果 obj 为 null,那么将返回 false。

3、obj 为 class 类的实例对象

integer integer = new integer(1);
system.out.println(integer instanceof integer);//true

  这没什么好说的,最普遍的一种用法。

4、obj 为 class 接口的实现类

  了解java 集合的,我们知道集合中有个上层接口 list,其有个典型实现类 arraylist

public class arraylist<e> extends abstractlist<e>
 implements list<e>, randomaccess, cloneable, java.io.serializable

  所以我们可以用 instanceof 运算符判断 某个对象是否是 list 接口的实现类,如果是返回 true,否则返回 false

arraylist arraylist = new arraylist();
system.out.println(arraylist instanceof list);//true

  或者反过来也是返回 true

list list = new arraylist();
system.out.println(list instanceof arraylist);//true

5、obj 为 class 类的直接或间接子类

  我们新建一个父类 person.class,然后在创建它的一个子类 man.class

public class person {
 
}

  man.class

public class man extends person{
 
}

  测试:

person p1 = new person();
person p2 = new man();
man m1 = new man();
system.out.println(p1 instanceof man);//false
system.out.println(p2 instanceof man);//true
system.out.println(m1 instanceof man);//true

  注意第一种情况, p1 instanceof man ,man 是 person 的子类,person 不是 man 的子类,所以返回结果为 false。

6、问题

  前面我们说过编译器会检查 obj 是否能转换成右边的class类型,如果不能转换则直接报错,如果不能确定类型,则通过编译,具体看运行时定。

  看如下几个例子:

person p1 = new person();
 
system.out.println(p1 instanceof string);//编译报错
system.out.println(p1 instanceof list);//false
system.out.println(p1 instanceof list<?>);//false
system.out.println(p1 instanceof list<person>);//编译报错

  按照我们上面的说法,这里就存在问题了,person 的对象 p1 很明显不能转换为 string 对象,那么自然 person 的对象 p1 instanceof string 不能通过编译,但为什么 p1 instanceof list 却能通过编译呢?而 instanceof list<person> 又不能通过编译了?

7、深究原理

  我们可以看java语言规范java se 8 版:

  如果用伪代码描述:

boolean result;
if (obj == null) {
 result = false;
} else {
 try {
 t temp = (t) obj; // checkcast
 result = true;
 } catch (classcastexception e) {
 result = false;
 }
}

  也就是说有表达式 obj instanceof t,instanceof 运算符的 obj 操作数的类型必须是引用类型或空类型; 否则,会发生编译时错误。

  如果 obj 强制转换为 t 时发生编译错误,则关系表达式的 instanceof 同样会产生编译时错误。 在这种情况下,表达式实例的结果永远为false。

  在运行时,如果 t 的值不为null,并且 obj 可以转换为 t 而不引发classcastexception,则instanceof运算符的结果为true。 否则结果是错误的

  简单来说就是:如果 obj 不为 null 并且 (t) obj 不抛 classcastexception 异常则该表达式值为 true ,否则值为 false 。

  所以对于上面提出的问题就很好理解了,为什么p1 instanceof string 编译报错,因为(string)p1 是不能通过编译的,而 (list)p1 可以通过编译。

8、instanceof 的实现策略

  javase 8 instanceof 的实现算法:

  

  1、obj如果为null,则返回false;否则设s为obj的类型对象,剩下的问题就是检查s是否为t的子类型;

  2、如果s == t,则返回true;

  3、接下来分为3种情况,之所以要分情况是因为instanceof要做的是“子类型检查”,而java语言的类型系统里数组类型、接口类型与普通类类型三者的子类型规定都不一样,必须分开来讨论。

  ①、s是数组类型:如果 t 是一个类类型,那么t必须是object;如果 t 是接口类型,那么 t 必须是由数组实现的接口之一;

  ②、接口类型:对接口类型的 instanceof 就直接遍历s里记录的它所实现的接口,看有没有跟t一致的;

  ③、类类型:对类类型的 instanceof 则是遍历s的super链(继承链)一直到object,看有没有跟t一致的。遍历类的super链意味着这个算法的性能会受类的继承深度的影响。

ps:下面看下instanceof关键字的作用是什么?

instanceof 运算符是用来在运行时判断对象是否是指定类及其父类的一个实例。

比较的是对象,不能比较基本类型

使用如下

package constxiong.interview;

/**
 * 测试 instanceof
 * @author constxiong
 * @date 2019-10-23 11:05:21
 */
public class testinstanceof {

 public static void main(string[] args) {
  a a = new a();
  aa aa = new aa();
  aaa aaa = new aaa();
  system.out.println(a instanceof a);//true
  system.out.println(a instanceof aa);//false
  system.out.println(aa instanceof aaa);//false
  system.out.println(aaa instanceof a);//true
 }
 
}

class a {
}

class aa extends a {
}

class aaa extends aa {
}

到此这篇关于java关键字instanceof用法解析的文章就介绍到这了,更多相关java关键字instanceof用法内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网