Java instanceof operator with Example

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. There are different types of operators are available in java which are given below:
1. Arithmetic Operators.
2. Unary Operators.
3. Assignment Operators.
4. Relational Operators.
5. Logical Operators.
6. BitWise Operators.
7. Ternary Operators.
8. InstanceOf Operators.



Java instanceof operator:


The instanceof operator in java compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface. The instanceof operator is also known as the type comparison operator because it compares the instance with type. The instanceof operator returns either true or false. The syntax of instanceof operator can be seen below:

 (object) instanceof (type) 

Here, the object is an instance of a class, and type is a class type. If an object is of a specified type or can be cast into a specified type, then the instanceof operators evaluate true. Otherwise, its result is false.

Java instanceof operator Example.

The following program defines a parent class (named Parent), a simple interface (named MyInterface), and a child class (named Child) that inherits from the parent and implements the interface.

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

        Parent obj1 = new Parent();
        Parent obj2 = new Child();

        System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent));
        System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child));
        System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface));
        System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent));
        System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child));
        System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface));
    }
}

class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}


Output:

Note: When using the instanceof operator, keep in mind that null is not an instance of anything. It returns false for null. Let's demonstrate instanceof operator for a null value.


 

class MyClass
{
public static void main(String args[])
{
MyClass m=null;
System.out.println(m instanceof MyClass);//false
}
}

Output:
  false 


Reference: Java Official Documentation.


You may also like these related posts:

1. Java Ternary Operator with example.
2. Java Bitwise Operator with example.
3. Java Logical Operator with example.
4. Java Relational Operator with example.
5. Java Assignment Operator with example.
6. Java Unary Operator with example.
7. Java Arithmetic Operator with example.
8. Basic data types in java.
9. Java Hello World program with example.
10. Top 100 java interview question and answer.
Java instanceof operator with Example Java instanceof operator with Example Reviewed by Prashant Srivastava on December 15, 2019 Rating: 5

No comments:

Powered by Blogger.