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

All Java Keywords 

  1. abstract Java Keyword
  2. assert Java Keyword
  3. boolean Java Keyword
  4. break Java Keyword
  5. byte Java Keyword
  6. case Java Keyword
  7. catch Java Keyword
  8. char Java Keyword
  9. class Java Keyword
  10. continue Java Keyword
  11. default Java Keyword
  12. do Java Keyword
  13. double Java Keyword
  14. else Java Keyword
  15. enum Java Keyword
  16. extends Java Keyword
  17. final Java Keyword
  18. finally Java Keyword
  19. float Java Keyword
  20. for Java Keyword
  21. if Java Keyword
  22. implements Java Keyword
  23. import Java Keyword
  24. instanceof Java Keyword
  25. int Java Keyword
  26. interface Java Keyword
  27. long Java Keyword
  28. native Java Keyword
  29. new Java Keyword
  30. package Java Keyword
  31. private Java Keyword
  32. protected Java Keyword
  33. public Java Keyword
  34. return Java Keyword
  35. short Java Keyword
  36. static Java Keyword
  37. strictfp Java Keyword
  38. super Java Keyword
  39. switch Java Keyword
  40. synchronized Java Keyword
  41. this Java Keyword
  42. throw Java Keyword
  43. throws Java Keyword
  44. transient Java Keyword
  45. try Java Keyword
  46. void Java Keyword
  47. volatile Java Keyword
  48. while Java Keyword

Comments