多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Java `instanceof`运算符 > 原文: [https://howtodoinjava.com/oops/java-instanceof/](https://howtodoinjava.com/oops/java-instanceof/) **Java 实例**运算符(也称为类型比较[运算符](https://howtodoinjava.com/java/basics/operators-in-java/))用于测试对象是否为指定类型(类,子类或接口)的实例。 它返回: * `true` - 如果变量是指定类的实例,则它是父类或实现指定接口或父接口 * `false` - 如果变量不是类的实例或接口的实现; 或变量为空 ## 1\. Java `instanceof`语法 `instanceof`运算符将变量测试为指定的类型。 变量写在运算符的左侧,类型在运算符的右侧。 ```java //<object-reference> instanceof TypeName boolean value = var instanceof ClassType; //or if(var instanceof ClassType) { //perform some action } ``` ## 2\. Java `instanceof`示例 让我们看一个示例,以充分了解`instanceof`运算符用于比较类型的用法。 在此示例中,我们使用[`ArrayList`](https://howtodoinjava.com/java-arraylist/) 类测试其类型信息。 ```java import java.util.AbstractList; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; public class Main { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<>(); System.out.println(arrayList instanceof ArrayList); //true System.out.println(arrayList instanceof AbstractList); //true System.out.println(arrayList instanceof List); //true System.out.println(arrayList instanceof Collection); //true System.out.println(null instanceof ArrayList); //false //System.out.println(arrayList instanceof LinkedList); //Does not compile } } ``` 程序输出。 ```java true true true true false ``` ## 3\. Java `instanceof`和数组 在 Java 中,数组也被视为对象,并具有与之关联的字段和方法。 因此,我们也可以将`instanceof`运算符与[数组](https://howtodoinjava.com/java-array/)一起使用。 * **基本数组**是`Object`和自身类型的实例。 例如`int[]`是`Object`和`int[]`的类型。 两种比较均返回`true`。 * **对象数组**是对象,对象数组,类类型数组,父类类型数组的类型。 例如`Integer[]`是`Object`,`Object[]`,`Integer[]`和`Number []`(`Integer extends Number`)的类型。 ```java import java.util.AbstractList; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; public class Main { public static void main(String[] args) { int[] intArr = new int[3]; float[] floatArr = new float[3]; Integer[] intObjArr = new Integer[3]; Float[] floatObjArr = new Float[3]; String[] stringArr = new String[3]; System.out.println(intArr instanceof Object); //true System.out.println(intArr instanceof int[]); //true System.out.println(floatArr instanceof Object); //true System.out.println(floatArr instanceof float[]); //true System.out.println(intObjArr instanceof Object); //true System.out.println(intObjArr instanceof Object[]); //true System.out.println(intObjArr instanceof Integer[]); //true System.out.println(intObjArr instanceof Number[]); //true System.out.println(floatObjArr instanceof Float[]); //true System.out.println(stringArr instanceof String[]); //true } } ``` 程序输出: ```java true true true true true true true true true true ``` ## 4\. 使用`instanceof`正确进行类型转换 一个使用`instanceof`运算符的现实示例可以将变量类型转换为另一种类型。`instanceof`运算符有助于在运行时避免`ClassCastException`。 考虑以下示例,其中我们尝试将列表类型转换为`LinkedList`类,其中原始变量的类型为`ArrayList`。 它将抛出`ClassCastException`。 ```java List<String> list = new ArrayList<>(); LinkedList<String> linkedList = (LinkedList<String>) list; ``` 为了正确地转换变量,我们可以使用`instanceof`运算符。 它不会导致`ClassCastException`。 ```java List<String> list = new ArrayList<>(); if(list instanceof LinkedList) { LinkedList<String> linkedList = (LinkedList<String>) list; //application code } ``` 向我提供有关用于类型比较的 **Java `instanceof`运算符**的问题。 学习愉快!