instanceof

164 阅读3分钟

instanceof 1、obj 必须为引用类型,不能是基本类型 2、obj 为 null 3、obj 为 class 类的实例对象 4、obj 为 class 接口的实现类 5、obj 为 class 类的直接或间接子类 6、问题 7、深究原理 8、instanceof 的实现策略 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 类型的描述在官方文档:docs.oracle.com/javase/spec… 有一些介绍。一般我们知道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 extends AbstractList implements List, 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 的对象 p1 很明显不能转换为 String 对象,那么自然 Person 的对象 p1 instanceof String 不能通过编译,但为什么 p1 instanceof List 却能通过编译呢?而 instanceof List 又不能通过编译了?