instanceof operator in java

Hi everyone, in this blog post, I will be describing about instanceof operator in java. Instanceof operator is mainly used to specify whether an object is an instance of a class. It allows to determine the type of the object. It is a boolean operator which returns true if the object is an instance of the specified class.  Instanceof operator is a useful tool when there is a collection of objects and we are not sure why they are used.

Here is a sample program which will give a better idea about the instanceof operator.

package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class InstanceOfExample {

    public static void main(String[] args) {
        List myList = new ArrayList<>();
        Map myMap = new HashMap<>();
        Set mySet = new LinkedHashSet<>();
        
        System.out.println("myList instanceOf List = "+(myList instanceof List));
        System.out.println("myMap instanceOf Map = "+(myMap instanceof Map));
        System.out.println("mySet instanceOf Set = "+(mySet instanceof Set));
        System.out.println();
        
        System.out.println("myList instanceOf ArrayList = "+(myList instanceof ArrayList));
        System.out.println("myMap instanceOf HashMap = "+(myMap instanceof HashMap));
        System.out.println("mySet instanceOf LinkedHashSet = "+(mySet instanceof LinkedHashSet));
        System.out.println();
        
        System.out.println("myList instanceOf Map = "+(myList instanceof Map));
        System.out.println("myMap instanceOf Set = "+(myMap instanceof Set));
        System.out.println("mySet instanceOf List = "+(mySet instanceof List));
        System.out.println();
        
        //Every Class is an instanceof Object
        System.out.println("myList instanceOf List = "+(myList instanceof Object));
        System.out.println("myMap instanceOf Map = "+(myMap instanceof Object));
        System.out.println("mySet instanceOf Set = "+(myList instanceof Object));
        
        }
}

The output of the above program is as follows :

myList instanceOf List = true
myMap instanceOf Map = true
mySet instanceOf Set = true

myList instanceOf ArrayList = true
myMap instanceOf HashMap = true
mySet instanceOf LinkedHashSet = true

myList instanceOf Map = false
myMap instanceOf Set = false
mySet instanceOf List = false

myList instanceOf List = true
myMap instanceOf Map = true
mySet instanceOf Set = true


From this program, we can see that object of a class List is an instance of HashMap and an instance of a class which is implemented by HashMap.

To summarize, following are some of the important points about instanceof operator.

  • instanceof operator is a boolean operator which returns true if an object is an instance of a class.
  • Every object of a class is an instance of that class.
  • Every object of a class is an instance of the superclass of that class.
  • Null is not an instance of any class.
  • Every object of class is an instance of Object.
That was all about instanceof operator. I hope you liked it. Please comment below if you have any doubts or want to add some more points. Every doubt will be cleared as soon as possible.

Thank you for reading the post.

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++