Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Introduction

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

2. What is the instanceof Operator?

instanceof is a binary operator used 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 type comparison operator because it compares the instance with type.

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

The instanceof operator's basic syntax is:

(object) instanceof (type)

Let's see a basic example for the instanceof operator. First, let's create a class Round:

public class Round {
    // implementation details
}

Next, let's create a class Ring that extends Round:

public class Ring extends Round {
    // implementation details
}

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

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

3. How Does the instanceof Operator Work?

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.

To demonstrate this, let's create a Shape interface:

public interface Shape {
    // implementation details
}

Let's also create a class Circle that implements the Shape interface and also extends the Round class:

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

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

@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:

@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:

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

The instanceof operator cannot be used if there is no relationship between the object that is being compared and the type it is being compared with.

Let's create a new class Triangle that implements Shape but has no relationship with Circle:

public class Triangle implements Shape {
    // implementation details
}

Now, if we use instanceof to check if a Circle is an instance of 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 the Triangle classes:

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

4. Using instanceof with the Object Type

In Java, every class implicitly inherits from the Object class. Therefore, using the instanceof operator with the Object type will always evaluate to 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

If we use the instanceof operator on any object that is null, it returns false. Also, no null check is needed when using an instanceof operator.

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

6. instanceof and Generics

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

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 are only allowed to use instanceof along with reified types in Java. A type is reified if its type information is present at runtime.

The reified types in Java are as follows:

  • 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<?>:

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

7. Conclusion

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

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!