InstanceOf Operator Tutorial

The first thing that we need to understand is the definition of a supertype. A supertype is a class or interface that is higher up the inheritance hierarchy. The instanceof operator compares an object(instance) to the Name of a supertype and returns a boolean result. The instanceof operator will also return true if the instance is of the same class name.
(reference variable) instanceof (Name of supertype)
String s = "Hello World";
s instanceof Object;
// true

I was going to make this tutorial a while back, but I decided not to because I really don't like the instanceof operator. It serves a useful purpose here and there, but it should only be used as last resort in most cases. I can't easily explain why, but as one becomes more fluent in the language it will become apparent. I'll give a short example of a bad use of the instanceof operator towards the end of the code below.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md InstanceofOperator
C:\Java>cd InstanceofOperator

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

class InstanceofOperator {
    public static void main(String args[]) {
        GiftBox giftbox = new GiftBox();
        CerealBox cerealbox = new CerealBox();
        Box box = new Box();

        System.out.println("giftbox instanceof GiftBox = " + (giftbox instanceof GiftBox));
        System.out.println("giftbox instanceof Box = " + (giftbox instanceof Box));
        System.out.println("giftbox instanceof Object = " + (giftbox instanceof Object));
        //System.out.println("giftbox instanceof CerealBox = " + (giftbox instanceof CerealBox));
        //System.out.println("giftbox instanceof Number = " + (giftbox instanceof Number));
        //System.out.println("giftbox instanceof box = " + (giftbox instanceof box));

        System.out.println("cerealbox instanceof Cerealbox = " + (cerealbox instanceof CerealBox));
        System.out.println("cerealbox instanceof Box = " + (cerealbox instanceof Box));
        System.out.println("cerealbox instanceof Object = " + (cerealbox instanceof Object));

        System.out.println("box instanceof Box = " + (box instanceof Box));
        System.out.println("box instanceof Object = " + (box instanceof Object));
        System.out.println("box instanceof CerealBox = " + (box instanceof CerealBox));
        System.out.println("box instanceof GiftBox = " + (box instanceof GiftBox));

        Box birthday = new GiftBox();
        Box cornsplotions = new CerealBox();
        Box brownbox = new Box();



    static void boxInfo(Box b) { 
        //example of bad usage
        if (b instanceof GiftBox) {
        } else if (b instanceof CerealBox) {
        } else {
            System.out.println("I am holding misc contents");


class Box {
    void displayBoxType() {
        System.out.println("Just a regular old box");

class GiftBox extends Box {
    void displayBoxType() {
        System.out.println("I\'m a gift box!");
    void displaySweater() {
        System.out.println("I'm hiding an ugly sweater.");

class CerealBox extends Box {
    void displayBoxType() {
        System.out.println("I\'m a cereal box!");
    void displayCereal() {
        System.out.println("I'm halfway filled with processed corn, riboflavin, and thiamin hydrochloride. Yummy yummy!");

Now switch back to the command prompt (CMD) and type in javac and press Enter.
Now type in java InstanceofOperator and press Enter.

C:\Java\InstanceofOperator>java InstanceofOperator
I'm a gift box! I'm prepped and ready to hold an ugly sweater.
giftbox instanceof GiftBox = true
giftbox instanceof Box = true
giftbox instanceof Object = true

I'm a cereal box! I'm halfway filled with processed corn, riboflavin, and thiamin hydrochloride. Yummy yummy!
cerealbox instanceof CerealBox = true
cerealbox instanceof Box = true
cerealbox instanceof Object = true

Just a regular old box
box instanceof Box = true
box instanceof Object = true
box instanceof CerealBox = false
box instanceof GiftBox = false

Final thoughts