0
0
JavaConceptBeginner · 3 min read

What is instanceof Operator in Java: Explanation and Examples

The instanceof operator in Java checks if an object is an instance of a specific class or implements a particular interface. It returns true if the object matches the type, otherwise false. This helps safely test an object's type before using it.
⚙️

How It Works

The instanceof operator works like a type-checking tool. Imagine you have a box and you want to know if it contains a certain kind of toy. Instead of opening the box and guessing, you ask if the box "is a" toy of that kind. In Java, instanceof asks if an object belongs to a certain class or interface.

It returns true if the object is exactly that type or a subtype (child class). If the object is null or not related to that type, it returns false. This helps avoid errors when you want to use features specific to that type.

💻

Example

This example shows how instanceof checks if an object is a String or an Integer.

java
public class InstanceofExample {
    public static void main(String[] args) {
        Object obj1 = "Hello";
        Object obj2 = Integer.valueOf(123);

        if (obj1 instanceof String) {
            System.out.println("obj1 is a String");
        } else {
            System.out.println("obj1 is NOT a String");
        }

        if (obj2 instanceof Integer) {
            System.out.println("obj2 is an Integer");
        } else {
            System.out.println("obj2 is NOT an Integer");
        }

        if (obj2 instanceof String) {
            System.out.println("obj2 is a String");
        } else {
            System.out.println("obj2 is NOT a String");
        }
    }
}
Output
obj1 is a String obj2 is an Integer obj2 is NOT a String
🎯

When to Use

Use instanceof when you have an object but are not sure what exact type it is. This is common when working with general types like Object or interfaces. It helps you avoid errors by checking the type before casting or calling methods.

For example, in a program that handles different shapes, you might check if a shape is a circle before calling circle-specific methods. Or when reading data from a source that can return different object types, instanceof helps you handle each type safely.

Key Points

  • instanceof returns true if the object is an instance of the specified class or interface.
  • It returns false if the object is null or not related to the type.
  • It helps avoid ClassCastException by checking type before casting.
  • Works with class inheritance and interfaces.
  • Commonly used in polymorphism and safe type handling.

Key Takeaways

The instanceof operator checks if an object belongs to a specific class or interface.
It returns true if the object matches the type, false otherwise, including when the object is null.
Use instanceof to safely test an object's type before casting or calling type-specific methods.
It helps prevent runtime errors like ClassCastException.
instanceof works with inheritance and interface implementations.