instanceof关键字详解
instanceof是Java中的一个双目运算符,也是Java中保留的关键字。
在Java中,可以使用instanceof关键字判断一个对象是否为一个类(或接口,抽象类,父类)的实例。
用法如下:
lean result = obj instanceof Class
其中,obj 是一个对象,Class 表示一个类或接口。obj 是 class 类(或接口)的实例或者子类实例时,结果 result 返回 true,否则返回 false。
下面介绍 Java instanceof 关键字的几种用法。
1)声明一个 class 类的对象,判断 obj 是否为 class 类的实例对象,如以下代码:
Integer integer = new Integer(1);
System.out.println(integer instanceof Integer); // true
2)声明一个 class 接口实现类的对象 obj,判断 obj 是否为 class 接口实现类的实例对象,如以下代码:
//Java 集合中的 List 接口有个典型实现类 ArrayList。
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
所以我们可以用 instanceof 运算符判断 ArrayList 类的对象是否属于 List 接口的实例,如果是返回 true,否则返回 false。
ArrayList arrayList = new ArrayList();
System.out.println(arrayList instanceof List); // true
3)obj 是 class 类的直接或间接子类
public class Person {
}
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
第 4 行代码中,Man 是 Person 的子类,Person 不是 Man 的子类,所以返回结果为 false。
值得注意的是 obj 必须为引用类型,不能是基本类型。例如以下代码:
int i = 0;
System.out.println(i instanceof Integer); // 编译不通过
System.out.println(i instanceof Object); // 编译不通过
所以,instanceof 运算符只能用作对象的判断。
当 obj 为 null 时,直接返回 false,因为 null 没有引用任何对象。
System.out.println(null instanceof Object); // false
当 class 为 null 时,会发生编译错误,错误信息如下:
Syntax error on token "null", invalid ReferenceType
所以 class 只能是类或者接口。
编译器会检查 obj 能否转换成右边的 class 类型,如果不能转换则直接报错,如果不能确定类型,则通过编译,进入执行。