Summary: In this tutorial, you will learn the use of instanceof operator in Java with the help of the examples.

instanceof in Java

In Java, the instanceof operator is used to determine if an object is an instance of a particular class or implements a particular interface.

Here is an example of how to use the instanceof operator:

String str = "Hello, World!";
if (str instanceof String) {
    System.out.println("str is an instance of String");
}

In this example, the instanceof operator is used to check if the str variable is an instance of the String class.

Since str is a String object, the condition will evaluate to true and the message will be printed.

Here is another example of how instanceof can be used:

String str = "Hello";

if (str instanceof Object) {
    // the code in this block will be executed because str is an instance of Object
}

In the example above, the instanceof operator is used to test whether the str object is an instance of the Object class.

Since all objects in Java are descendants of the Object class, the test evaluates to true and the code inside the block is executed.

instanceof in Inheritance

In the context of inheritance, instanceof can be used to test whether an object is an instance of a particular class or one of its subclasses.

For example, consider the following code:

class Animal { }

class Dog extends Animal { }

class Cat extends Animal { }

Dog dog = new Dog();
Cat cat = new Cat();

if (dog instanceof Animal) {
    System.out.println("dog is an instance of Animal");
}

if (cat instanceof Animal) {
    System.out.println("cat is an instance of Animal");
}

In this example, the Dog and Cat classes are both subclasses of the Animal class. The dog and cat variables are references to Dog and Cat objects, respectively.

The instanceof operator is used to check if the dog and cat variables are instances of the Animal class. Since both Dog and Cat are subclasses of Animal, the conditions will both evaluate to true and the messages will be printed.

The instanceof operator can also be used to check if an object is an instance of a specific subclass:

if (dog instanceof Dog) {
    System.out.println("dog is an instance of Dog");
}

if (cat instanceof Cat) {
    System.out.println("cat is an instance of Cat");
}

In this example, the instanceof operator is used to check if the dog and cat variables are instances of the Dog and Cat classes, respectively.

Since the dog and cat variables are references to Dog and Cat objects, the conditions will both evaluate to true and the messages will be printed.

instanceof with Interface

instanceof can also be used to test whether an object is an implementation of a particular interface.

Here is an example that demonstrates how this can be done:

public interface Shape {
    double getArea();
}

public class Circle implements Shape {
    private double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }
    
    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
}

public class Rectangle implements Shape {
    private double width;
    private double height;
    
    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    
    @Override
    public double getArea() {
        return width * height;
    }
}

// usage
Shape shape = new Circle(5);
if (shape instanceof Circle) {
    System.out.println("shape is a Circle");
}

shape = new Rectangle(10, 5);
if (shape instanceof Rectangle) {
    System.out.println("shape is a Rectangle");
}

In this example, the Shape interface has a single method getArea() which returns the area of the shape.

The Circle and Rectangle classes both implement the Shape interface and provide their own implementations of the getArea() method.

The instanceof operator is used to check the type of the shape variable.

If shape is an instance of Circle, the message “shape is a Circle” is printed. If shape is an instance of Rectangle, the message “shape is a Rectangle” is printed.

instanceof with Primitive Types

instanceof does not work with primitive types in Java. It can only be used to test the type of objects.

For example, the following code will not compile because instanceof is being used with a primitive type:

int x = 10;

if (x instanceof Integer) {
    // this code will not compile because x is a primitive int, not an object of type Integer
}

To test the type of a primitive value, you can use the typeof operator in other programming languages. However, Java does not have a typeof operator.

To test the type of a primitive value in Java, you can use the instanceof operator. However, you must first wrap the primitive value in an object of the corresponding wrapper class.

For example, to test the type of an int value, you can wrap it in an Integer object and then use instanceof to test the type of the object:

int x = 10;
Integer y = new Integer(x);

if (y instanceof Integer) {
    // the code in this block will be executed because y is an object of type Integer
}

Alternatively, you can use the getClass() method to test the type of an object:

int x = 10;
Integer y = new Integer(x);

if (y.getClass() == Integer.class) {
    // the code in this block will be executed because y is an object of type Integer
}

Leave a Reply