Java instanceof Operator

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

Leave a Reply

Your email address will not be published.