instanceof Java Keyword with Examples

The Java instanceof keyword is used to test whether the object is an instance of the specified type (class or subclass or interface).

  • Syntex:
  • obj instanceOf Object
  • The left side is the instance and right side is the Java class name. Java instanceof operator returns a boolean result.
  • The instanceof in Java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has a null value, it returns false.
  • instanceof Java Keyword Example 1
package com.javaguides.corejava.keywords.instanceofkeyword;

/**
 * Class demonstrates the usage of instanceof keyword
 * @author Ramesh Fadatare
 *
 */
public class InstanceOfKeyword {

    public static void main(String[] args) {
        Pizza pizza = new VegPizza();
        Pizza nonPizza = new NonVegPizza();
        test(pizza);
        test(nonPizza);
    }

    private static void test(Pizza pizza) {
        if (pizza instanceof VegPizza) {
            pizza.bake();
        }

        if (pizza instanceof NonVegPizza) {
            pizza.bake();
        }
    }
}

interface Pizza {
    public void bake();
}

class VegPizza implements Pizza {

    @Override
    public void bake() {
        System.out.println("Bake Veg Pizza");
    }
}

class NonVegPizza implements Pizza {

    @Override
    public void bake() {
        System.out.println("Bake Non-Veg Pizza");
    }
}

Output:

Bake Veg Pizza
Bake Non-Veg Pizza

Note that usage of instanceof keyword:

    private static void test(Pizza pizza) {
        if (pizza instanceof VegPizza) {
            pizza.bake();
        }

        if (pizza instanceof NonVegPizza) {
            pizza.bake();
        }
    }
    

instanceof Keyword Example 2

Let’s see the real use of instanceof keyword by the example given below.

package com.javaguides.corejava.keywords.instanceofkeyword;

public class InstanceOfKeyword2 {
    public static void main(String[] args) {
        Operations addOperation = new AddOperation();
        Operations subOperation = new SubOperation();
        Operations mulOperation = new MultiplyOperation();
        Operations divOperation = new AddOperation();

        Calculate calculate = new Calculate();

        calculate.process(addOperation);
        calculate.process(subOperation);
        calculate.process(mulOperation);
        calculate.process(divOperation);

        System.out.println(addOperation instanceof Operations); // true
        System.out.println(subOperation instanceof Operations); // true
        System.out.println(mulOperation instanceof Operations); // true
        System.out.println(divOperation instanceof Operations); // true

        System.out.println(addOperation instanceof Object); // true

        System.out.println(new AddOperation() instanceof Operations); // true
    }
}

interface Operations {
    public int doOperation(int num1, int num2);
}

class AddOperation implements Operations {

    @Override
    public int doOperation(int num1, int num2) {
        return (num1 + num2);
    }
}

class SubOperation implements Operations {

    @Override
    public int doOperation(int num1, int num2) {
        return (num1 - num2);
    }
}

class MultiplyOperation implements Operations {

    @Override
    public int doOperation(int num1, int num2) {
        return (num1 * num2);
    }
}

class DivisionOperation implements Operations {

    @Override
    public int doOperation(int num1, int num2) {
        return (num1 / num2);
    }
}

class Calculate {

    public void process(Operations operations) {
        if (operations instanceof AddOperation) {
            operations.doOperation(1, 2);
        }

        if (operations instanceof SubOperation) {
            operations.doOperation(2, 1);
        }

        if (operations instanceof MultiplyOperation) {
            operations.doOperation(2, 2);
        }

        if (operations instanceof DivisionOperation) {
            operations.doOperation(2, 1);
        }
    }
}

Java instanceof with an Array

Java arrays are also Object, but instanceof operator works differently when it’s used with an array.
For example:

// Java instanceof with array

        int[] intArray = {
            1,
            2,
            3,
            4,
            5
        };
        System.out.println(intArray instanceof Object);

Output:

true

But if we try something like below:

        String[] strArray = {
            "ABC",
            "XYZ"
        };
        System.out.println(strArray instanceof String);

Then we get a compile-time error as Incompatible conditional operand types String[] and String. However, below the use of instanceof operator works fine.

        String[] strArray = {
            "ABC",
            "XYZ"
        };
        System.out.println(strArray instanceof String[]);

Java instanceof keyword with Collection example

 // Java instanceof with array
 
 List < String > list = Arrays.asList("ABC", "XYZ");
 System.out.println(list instanceof Collection);

Output:

true