Automation QA Testing Course Content

Java instanceof Operator

The instanceof operator in Java is used to check whether an object is an instance of a particular class or not.

Its syntax is

objectName instanceOf className;

Here, if objectName is an instance of className, the operator returns true. Otherwise, it returns false.


Example: Java instanceof

class Main {

  public static void main(String[] args) {

    // create a variable of string type
    String name = "Programiz";
    
    // checks if name is instance of String
    boolean result1 = name instanceof String;
    System.out.println("name is an instance of String: " + result1);

    // create an object of Main
    Main obj = new Main();

    // checks if obj is an instance of Main
    boolean result2 = obj instanceof Main;
    System.out.println("obj is an instance of Main: " + result2);
  }
}

Output

name is an instance of String: true
obj is an instance of Main: true

In the above example, we have created a variable name of the String type and an object obj of the Main class.

Here, we have used the instanceof operator to check whether name and obj are instances of the String and Main class respectively. And, the operator returns true in both cases.

Note: In Java, String is a class rather than a primitive data type. To learn more, visit Java String.


Java instanceof during Inheritance

We can use the instanceof operator to check if objects of the subclass is also an instance of the superclass. For example,

// Java Program to check if an object of the subclass
// is also an instance of the superclass

// superclass
class Animal {
}

// subclass
class Dog extends Animal {
}

class Main {
  public static void main(String[] args) {

    // create an object of the subclass
    Dog d1 = new Dog();

    // checks if d1 is an instance of the subclass
    System.out.println(d1 instanceof Dog);        // prints true

    // checks if d1 is an instance of the superclass
    System.out.println(d1 instanceof Animal);     // prints true
  }
}

In the above example, we have created a subclass Dog that inherits from the superclass Animal. We have created an object d1 of the Dog class.

Inside the print statement, notice the expression,

d1 instanceof Animal

Here, we are using the instanceof operator to check whether d1 is also an instance of the superclass Animal.


Java instanceof in Interface

The instanceof operator is also used to check whether an object of a class is also an instance of the interface implemented by the class. For example,

// Java program to check if an object of a class is also
//  an instance of the interface implemented by the class

interface Animal {
}

class Dog implements Animal {
}

class Main {
  public static void main(String[] args) {

    // create an object of the Dog class
    Dog d1 = new Dog();

    // checks if the object of Dog
    // is also an instance of Animal
    System.out.println(d1 instanceof Animal);  // returns true
  }
}

In the above example, the Dog class implements the Animal interface. Inside the print statement, notice the expression,

d1 instanceof Animal

Here, d1 is an instance of Dog class. The instanceof operator checks if d1 is also an instance of the interface Animal.

Note: In Java, all the classes are inherited from the Object class. So, instances of all the classes are also an instance of the Object class.

In the previous example, if we check,

d1 instanceof Object

The result will be true.

You probably remember that the
instanceof operator returns true if the check evaluates to true, or false if the expression is false. Accordingly, it usually occurs in all sorts of conditional expressions (if…else). Let's start with some simpler examples:
public class Main {

   public static void main(String[] args) {

       Integer x = new Integer(22);

       System.out.println(x instanceof Integer);
   }
}
What do you think will be displayed on the console? Well, it's obvious here.:) The x object is an Integer, so the result will be true. Console output: True Let's try checking whether it's a String:
public class Main {

   public static void main(String[] args) {

       Integer x = new Integer(22);

       System.out.println(x instanceof String); // Error!
   }
}
We got an error. And pay attention: the compiler generated the error before running the code! It immediately saw that Integer and String cannot be automatically converted to each other and are not related through inheritance. Consequently, an Integer object is not created based on String. This is convenient and helps avoid strange runtime errors, so the compiler helped us out here :) Now let's try to consider examples that are more difficult. Since we mentioned inheritance, let's work with the following small system of classes:
public class Animal {

}

public class Cat extends Animal {

}

public class MaineCoon extends Cat {

}
We already know how instanceof behaves when we check whether an object is an instances of a class, but what happens if we consider the parent-child relationship? How the instanceof operator works - 2For example, what do you think these expressions will yield:
public class Main {

   public static void main(String[] args) {

       Cat cat = new Cat();

       System.out.println(cat instanceof Animal);

       System.out.println(cat instanceof MaineCoon);

   }
}
Output: True False The main question that needs to be answered is exactly how instanceof interprets 'object created based on a class'? 'cat instanceof Animal' evaluates to true, but surely we can find fault with that wording. Why is a Cat object created based on the Animal class? Isn't it created based on its own class alone? The answer is simple enough, and you may have already thought of it. Remember the order in which constructors are called and variables are initialized when creating an object. We already covered this topic in the article about class constructors. Here's an example from that lesson:
public class Animal {

   String brain = "Initial value of brain in the Animal class";
   String heart = "Initial value of heart in the Animal class";

   public static int animalCount = 7700000;

   public Animal(String brain, String heart) {
       System.out.println("Animal base class constructor is running");
       System.out.println("Have the variables of the Animal class already been initialized?");
       System.out.println("Current value of static variable animalCount = " + animalCount);
       System.out.println("Current value of brain in the Animal class = " + this.brain);
       System.out.println("Current value of heart in the Animal class = " + this.heart);

       this.brain = brain;
       this.heart = heart;
       System.out.println("Animal base class constructor is done!");
       System.out.println("Current value of brain = " + this.brain);
       System.out.println("Current value of heart = " + this.heart);
   }
}

public class Cat extends Animal {

   String tail = "Initial value of tail in the Cat class";

   static int catCount = 37;

   public Cat(String brain, String heart, String tail) {
       super(brain, heart);
       System.out.println("The Cat class constructor has started (The Animal constructor already finished)");
       System.out.println("Current value of static variable catCount = " + catCount);
       System.out.println("Current value of tail = " + this.tail);
       this.tail = tail;
       System.out.println("Current value of tail = " + this.tail);
   }

   public static void main(String[] args) {
       Cat cat = new Cat("Brain", "Heart", "Tail");
   }
}
And if you run it in the IDE, the console output will look like this: Animal base class constructor is running Have the variables of the Animal class already been initialized? Current value of static variable animalCount = 7700000 Current value of brain in the Animal class = Initial value of brain in the Animal class Current value of heart in the Animal class = Initial value of heart in the Animal class Have the variables of the Cat class already been initialized? Current value of static variable catCount = 37 Animal base class constructor is done! Current value of brain = Brain Current value heart = Heart The cat class constructor has started (The Animal constructor already finished) Current value of static variable catCount = 37 Current value of tail = Initial value of tail in the Cat class Current value of tail = Tail Now do you remember?:) The constructor of the base class, if there is a base class, is always called first when creating an object. The instanceof operator is guided by this principle when trying to determine whether an A object was created based on a B class. If the base class constructor is called, then there can be no doubt. With the second check, everything is simpler:
System.out.println(cat instanceof MaineCoon);
The MaineCoon constructor was not called when the Cat object was created, which makes sense. After all, MaineCoon is a descendant of Cat, not an ancestor. And it is not a template for Cat. Okay, I think we're clear on that. But what happens if we do this?:
public class Main {

   public static void main(String[] args) {

       Cat cat = new MaineCoon();

       System.out.println(cat instanceof Cat);
       System.out.println(cat instanceof MaineCoon);


   }
}
Hmm... now that's harder. Let's talk it over. We have a Cat variable to which we assigned a MaineCoon object. By the way, why does that even work? We can do that, right? Yes, we can. After all, every MaineCoon is a cat. If that's not entirely clear, remember the example of widening primitive types:
public class Main {

   public static void main(String[] args) {

       long x = 1024;

   }
}
The number 1024 is a short: it easily fits into a long variable, since there are enough bytes to accommodate it (remember the example with the dolls?). A descendant object can always be assigned to an ancestor variable. For now, just remember this, and in subsequent lessons we'll analyze how it works. So what does our example output?
Cat cat = new MaineCoon();
System.out.println(cat instanceof Cat);
System.out.println(cat instanceof MaineCoon);
What will instanceof check? our Cat variable or our MaineCoon object? The answer is this question is actually simple. You just need to read the definition of the operator again: The instanceof operator is needed to check whether an object referenced by an X variable was created based on some Y class. The instanceof operator tests the origin of an object, not the variable type. Thus, in this example, our program will display true in both cases: we have a MaineCoon object. Obviously, it was created based on the MaineCoon class, but it was created based on the Cat parent class as well!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.