Friday , July 21 2017
Home / java / Java instanceof Operator Example

Java instanceof Operator Example

 

In Java instanceof is an operator. It is used to check whether the given object is a particular instance type or not. It returns either true or false. If the given object is a particular type it returns true or else it returns false.

The Java instanceof operator is also called as comparison operator, because it compares the type of an object.

Java instanceof basic functionality :

Let us see the basic example for instanceof java


public class Instanceof_Demo {

    public static void main(String[] args) {
        String str = new String();
        System.out.println("is str instanceof Strng type ? " + (str instanceof String));
    }
}

Output :

is str instanceof Strng type ? true

Comparing Different Objects with Java instanceof :

By using the instanceof operator, we can compare the two different objects. But the important point here is, the two objects should be same type i.e., there should be some relation between the two objects (either child to parent or parent to child or same type) other wise compiler will generate compile time error.

Example for Java instanceof with different objects :

java instanceofOn the above diagram, the java.lang.Thread is a class. Which implements the java.lang.Runnable interface and it also extends from java.lang.Object. So we can compare the Thread class with Object type and Runnable type.

 
class InstnceOf_Relation {

    public static void main(String[] args) {
        Thread thread = new Thread();
        System.out.println("is thread instanceof Thread ? " + (thread instanceof Thread));
        System.out.println("is thread instanceof Runnable ? " + (thread instanceof Runnable));
        System.out.println("is thread instanceof Runnable ? " + (thread instanceof Object));
    }
}
Output : 
is thread instanceof Thread ? true 
is thread instanceof Runnable ? true 
is thread instanceof Runnable ? true 

Invalid comparison with java instanceof :

instanceof example wrongOn the above diagram, there is a relation between the java.lang.String to java.lang.Object and java.lang.Thread to java.lang.Object. But there is no relation between java.lang.String to Java.lang.Thread. We use the instanceof operator for this types. Other wise we will get compile error called incompatible types.

Example for Wrong Comparison :


class InstnceOf_Relation {

    public static void main(String[] args) {
        Thread thread = new Thread();

        System.out.println("is thread instance of String ? " + (thread instanceof String));
    }
}

InstnceOf_Relation.java:6: error: incompatible types: Thread cannot be converted to String

System.out.println("is thread instance of String ? "+(thread instanceof String));
^
1 error

The above example generates compilation error, because there is no relationship between the Thread and String classes. To use Java instanceof , there should be a relation between the two arguments.

Inheritance relation with Java instanceof operator :

We can use the instanceof operator to check the type of an abject in child to parent relationship also.

If we check the child class object with parent class type it will works fine, but when ever we check whether the parent object is child type or not by using instanceof operator, then always we will get false as result.

java instanceof example for Inheritance Relation :


class Person {
}

class Employee extends Person {

    public static void main(String[] args) {
        Employee e = new Employee();
        Person p = new Person();
        System.out.println("is Employee Person Type ? " + (e instanceof Person));
        System.out.println("is Person Employee Type ? " + (p instanceof Employee));
        Object object = new String("onlinetutorialspoint");
        System.out.println("is object instance of String ? " + (object instanceof String));
    }
}

Output :

is Employee Person Type ? true
is Person Employee Type ? false
is object instance of String ? true

instanceof operator on null values:

If we apply the type checking with instanceof operator on null values, the result is always false.


//null checking with instanceOf
class Instanceof_null {

    public static void main(String[] args) {
        System.out.println(null instanceof String);
        System.out.println(null instanceof Thread);
        System.out.println(null instanceof InstanceOf_null);
    }
}

Output :

false
false
false

On the above example, we compared null with String, Thread and Instanceof_null objects. For all comparisons we got false as a output.

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Binary Literals Java 7 Example Tutorials

Binary literals is one of the new feature added in Java 7. We already know …

Leave a Reply

Your email address will not be published. Required fields are marked *