How do I use the instanceof keyword?

To check whether an object is of a particular type (class or interface type) you can use instanceof operator. The instanceof operator is used only for object reference variable. x instanceof y can be read as x is-a y.

The instanceof returns true if the reference variable being tested is of the type being compared to. It will still return true if the object being compared is assignment compatible with the type on the right.

For interface type, an object is said to be of a particular interface type (meaning it will pass the instanceof test) if any of the object’s superclasses implement the interface.

package org.kodejava.basic;

interface Man {
}

public class InstanceofDemo {
    public static void main(String[] args) {
        Body body = new Body();
        Hand hand = new Hand();
        Nail nail = new Nail();
        Shoes shoe = new Shoes();

        if (body instanceof Man) {
            System.out.println("body is a Man");
        }

        if (hand instanceof Man) {
            System.out.println("hand is a Man too");
        }

        if (hand instanceof Body) {
            System.out.println("hand is a Body");
        }

        // it should be return false
        if (hand instanceof Nail) {
            System.out.println("hand is a Nail");
        } else {
            System.out.println("hand is not a Nail");
        }

        if (nail instanceof Man) {
            System.out.println("nail is a Man too");
        }

        if (nail instanceof Hand) {
            System.out.println("nail is a Hand");
        }
        if (nail instanceof Body) {
            System.out.println("nail is a Body too");
        }

        // it should return false, cause Shoes is not implements Man
        if (shoe instanceof Man) {
            System.out.println("shoe is a Man");
        } else {
            System.out.println("shoe is not a Man");
        }

        // compile error. cannot test against class in different
        // class hierarchies.
        //
        //if (shoe instanceof Body) {
        //}

    }

}

class Body implements Man {
}

// indirect implements Man
class Hand extends Body {
}

// indirect implements Man
class Nail extends Hand {
}

class Shoes {
}

The result of the code snippet above:

body is a Man
hand is a Man too
hand is a Body
hand is not a Nail
nail is a Man too
nail is a Hand
nail is a Body too
shoe is not a Man

How do I know the class of an object?

For instance, you have a collection of objects in an List object, and you want to do some logic based on the object’s class. This can easily be done using the instanceof operator. The operator returns true if an object is an instance of a specified class, if not it will return false.

The instanceof operator is most likely used when implementing an equals(Object o) method of an object to check if the compared object is from the same class.

package org.kodejava.lang;

import java.util.ArrayList;
import java.util.List;

public class InstanceOfExample {
    public static void main(String[] args) {
        Person person = new Person("John");
        Animal animal = new Animal("Highland");
        Thing thing = new Thing("Red");
        String text = "hello";
        Integer number = 1000;

        List<Object> list = new ArrayList<>();
        list.add(person);
        list.add(animal);
        list.add(thing);
        list.add(text);
        list.add(number);

        for (Object o : list) {
            if (o instanceof Person) {
                System.out.println("My name is " + ((Person) o).getName());
            } else if (o instanceof Animal) {
                System.out.println("I live in " + ((Animal) o).getHabitat());
            } else if (o instanceof Thing) {
                System.out.println("My color is " + ((Thing) o).getColor());
            } else if (o instanceof String) {
                System.out.println("My text is " + o.toString());
            } else if (o instanceof Integer) {
                System.out.println("My value is " + ((Integer) o));
            }
        }
    }
}

class Person {
    private final String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

class Animal {
    private final String habitat;

    public Animal(String habitat) {
        this.habitat = habitat;
    }

    public String getHabitat() {
        return habitat;
    }
}

class Thing {
    private final String color;

    public Thing(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }
}

The result of the code snippet above:

My name is John
I live in Highland
My color is Red
My text is hello
My value is 1000