Skip to content

Latest commit

 

History

History
161 lines (109 loc) · 4.25 KB

112.md

File metadata and controls

161 lines (109 loc) · 4.25 KB

Java instanceof运算符

原文: https://howtodoinjava.com/oops/java-instanceof/

Java 实例运算符(也称为类型比较运算符)用于测试对象是否为指定类型(类,子类或接口)的实例。

它返回:

  • true - 如果变量是指定类的实例,则它是父类或实现指定接口或父接口
  • false - 如果变量不是类的实例或接口的实现; 或变量为空

1. Java instanceof语法

instanceof运算符将变量测试为指定的类型。 变量写在运算符的左侧,类型在运算符的右侧。

//<object-reference> instanceof TypeName

boolean value = var instanceof ClassType;

//or

if(var instanceof ClassType) {
	//perform some action
}

2. Java instanceof示例

让我们看一个示例,以充分了解instanceof运算符用于比较类型的用法。 在此示例中,我们使用ArrayList 类测试其类型信息。

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
	}
}

程序输出。

true
true
true
true
false

3. Java instanceof和数组

在 Java 中,数组也被视为对象,并具有与之关联的字段和方法。 因此,我们也可以将instanceof运算符与数组一起使用。

  • 基本数组Object和自身类型的实例。 例如int[]Objectint[]的类型。 两种比较均返回true
  • 对象数组是对象,对象数组,类类型数组,父类类型数组的类型。 例如Integer[]ObjectObject[]Integer[]Number []Integer extends Number)的类型。
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
	}
}

程序输出:

true
true
true
true
true
true
true
true
true
true

4. 使用instanceof正确进行类型转换

一个使用instanceof运算符的现实示例可以将变量类型转换为另一种类型。instanceof运算符有助于在运行时避免ClassCastException

考虑以下示例,其中我们尝试将列表类型转换为LinkedList类,其中原始变量的类型为ArrayList。 它将抛出ClassCastException

List<String> list = new ArrayList<>();

LinkedList<String> linkedList = (LinkedList<String>) list;

为了正确地转换变量,我们可以使用instanceof运算符。 它不会导致ClassCastException

List<String> list = new ArrayList<>();

if(list instanceof LinkedList) 
{
	LinkedList<String> linkedList = (LinkedList<String>) list;

	//application code
}

向我提供有关用于类型比较的 Java instanceof运算符的问题。

学习愉快!