Java instanceof Operator – Java的操作者实例

最后修改: 2019年 1月 13日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this quick tutorial, we’ll learn about the instanceof operator in Java.

在这个快速教程中,我们将学习Java中的instanceof操作器。

2. What Is the instanceof Operator?

2.什么是instanceof操作符?

instanceof is a binary operator we use to test if an object is of a given type. The result of the operation is either true or false. It’s also known as a type comparison operator because it compares the instance with the type.

instanceof是一个二进制运算符,我们用来测试一个对象是否属于给定的类型。运算的结果是truefalse。它也被称为类型比较运算符,因为它将实例与类型进行比较。

Before casting an unknown object, the instanceof check should always be used. Doing this helps to avoid a ClassCastException at runtime.

casting一个未知对象之前,应该始终使用instanceof检查。这样做有助于避免在运行时出现ClassCastException

The instanceof operator’s basic syntax is:

instanceof操作符的基本语法是。

(object) instanceof (type)

Now let’s see a basic example for the instanceof operator. First, we’ll create a class Round:

现在让我们看看instanceof操作符的一个基本例子。首先,我们将创建一个Round类。

public class Round {
    // implementation details
}

Next, we’ll create a class Ring that extends Round:

接下来,我们将创建一个Ring类,它扩展了Round

public class Ring extends Round {
    // implementation details
}

We can use instanceof to check if an instance of Ring is of Round type:

我们可以使用instanceof来检查Ring的一个实例是否属于Round类型。

@Test
public void givenWhenInstanceIsCorrect_thenReturnTrue() {
    Ring ring = new Ring();
    Assert.assertTrue(ring instanceof Round);
}

3. How Does the instanceof Operator Work?

3.instanceof操作符如何工作?

The instanceof operator works on the principle of the is-a relationship. The concept of an is-a relationship is based on class inheritance or interface implementation.

instanceof操作符的工作原理是is-a关系。is-a关系的概念是基于类继承或接口实现。

To demonstrate this, we’ll create a Shape interface:

为了证明这一点,我们将创建一个Shape接口。

public interface Shape {
    // implementation details
}

We’ll also create a class Circle, which implements the Shape interface and also extends the Round class:

我们还将创建一个Circle,类,它实现了Shape接口,同时也扩展了Round类。

public class Circle extends Round implements Shape {
    // implementation details
}

The instanceof result will be true if the object is an instance of the type:

如果对象是一个类型的实例,instanceof结果将是true

@Test
public void givenWhenObjectIsInstanceOfType_thenReturnTrue() {
    Circle circle = new Circle();
    Assert.assertTrue(circle instanceof Circle);
}

It will also be true if the object is an instance of a subclass of the type:

如果对象是一个类型的子类的实例,它也将是true

@Test
public void giveWhenInstanceIsOfSubtype_thenReturnTrue() {
    Circle circle = new Circle();
    Assert.assertTrue(circle instanceof Round);
}

If the type is an interface, it will return true if the object implements the interface:

如果类型是一个接口,如果对象实现了该接口,它将返回true

@Test
public void givenWhenTypeIsInterface_thenReturnTrue() {
    Circle circle = new Circle();
    Assert.assertTrue(circle instanceof Shape);
}

The instanceof operator can’t be used if there’s no relationship between the object that’s being compared and the type it’s being compared with.

如果被比较的对象和被比较的类型之间没有关系,就不能使用 instanceof操作符。

We’ll create a new class, Triangle, that implements Shape, but has no relationship with Circle:

我们将创建一个新的类,Triangle,它实现了Shape,但与Circle没有关系。

public class Triangle implements Shape {
    // implementation details
}

Now if we use instanceof to check if a Circle is an instance of Triangle:

现在,如果我们使用instanceof来检查Circle是否是Triangle的一个实例。

@Test
public void givenWhenComparingClassInDiffHierarchy_thenCompilationError() {
    Circle circle = new Circle();
    Assert.assertFalse(circle instanceof Triangle);
}

We’ll get a compilation error because there’s no relationship between the Circle and Triangle classes:

我们会得到一个编译错误,因为CircleTriangle类之间没有关系。

java.lang.Error: Unresolved compilation problem:
  Incompatible conditional operand types Circle and Triangle

4. Using instanceof With the Object Type

4.使用instanceofObject类型

In Java, every class implicitly inherits from the Object class. Therefore, using the instanceof operator with the Object type will always evaluate to true:

在Java中,每个类都隐含地继承自Object类。因此,对Object类型使用instanceof操作符,其结果总是true

@Test
public void givenWhenTypeIsOfObjectType_thenReturnTrue() {
    Thread thread = new Thread();
    Assert.assertTrue(thread instanceof Object);
}

5. Using the instanceof Operator When an Object Is null

5.当一个对象是null时,使用instanceof操作符

If we use the instanceof operator on any object that’s null, it returns false. We also don’t need a null check when using an instanceof operator.

如果我们在任何对象上使用instanceof操作符,它将返回false。在使用instanceof操作符时,我们也不需要检查空。

@Test
public void givenWhenInstanceValueIsNull_thenReturnFalse() {
    Circle circle = null;
    Assert.assertFalse(circle instanceof Round);
}

6. instanceof and Generics

6.instanceof和泛型

Instance tests and casts depend on inspecting the type information at runtime. Therefore, we can’t use instanceof along with erased generic types.

实例测试和铸造依赖于在运行时检查类型信息。因此,我们不能将instanceof 删除的泛型类型一起使用。

For instance, if we try to compile the following snippet:

例如,如果我们尝试编译以下片段。

public static <T> void sort(List<T> collection) {
    if (collection instanceof List<String>) {
        // sort strings differently
    }
        
    // omitted
}

Then we get this compilation error:

然后我们得到这个编译错误。

error: illegal generic type for instanceof
        if (collection instanceof List<String>) {
                                      ^

Technically speaking, we’re only allowed to use instanceof along with reified types in Java. A type is reified if its type information is present at runtime.

从技术上讲,我们只允许在Java中使用instanceof和reified类型。如果一个类型的类型信息在运行时存在,它就是reified。

The reified types in Java are as follows:

Java中的重化类型如下。

  • Primitive types, like int
  • Non-generic classes and interfaces, like String or Random
  • Generic types in which all types are unbounded wildcards, like Set<?> or Map<?, ?>
  • Raw types, like List or HashMap
  • Arrays of other reifiable types, like String[], List[], or Map<?, ?>[]

Because generic type parameters aren’t reified, we can’t use them either:

因为通用类型参数没有被重构,我们也不能使用它们。

public static <T> boolean isOfType(Object input) {
    return input instanceof T; // won't compile
}

However, it’s possible to test against something like List<?>:

然而,可以针对类似List<?>的东西进行测试。

if (collection instanceof List<?>) {
    // do something
}

7. Conclusion

7.结语

In this brief article, we learned about the instanceof operator and how to use it. The complete code samples are available over on GitHub.

在这篇简短的文章中,我们了解了instanceof操作符以及如何使用它。完整的代码样本可在GitHub上找到。