instanceof operator vs isInstance() method in Java

instanceof operator and isInstance() method both are used for checking the class of the object. But main difference comes when we want to check the class of object dynamically. In this case isInstance() method will work. There is no way we can do this by instanceof operator.

instanceof operator and isInstance() method both return a boolean value. isInstance() method is a method of class Class in java while instanceof is a operator.
Consider an example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working of
// instanceof operator
public class Test
{
    public static void main(String[] args)
    {
        Integer i = new Integer(5);
  
        // prints true as i is instance of class
        // Integer
        System.out.println(i instanceof Integer);
    }
}

chevron_right


Output:

true

Now if we want to check the class of the object at run time, then we must use isInstance() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working of isInstance() 
// method 
public class Test 
    // This method tells us whether the object is an 
    // instance of class whose name is passed as a 
    // string 'c'. 
    public static boolean fun(Object obj, String c) 
                      throws ClassNotFoundException 
    
        return Class.forName(c).isInstance(obj); 
    
    
    // Driver code that calls fun() 
    public static void main(String[] args) 
                      throws ClassNotFoundException 
    
        Integer i = new Integer(5); 
    
        // print true as i is instance of class 
        // Integer 
        boolean b = fun(i, "java.lang.Integer"); 
    
        // print false as i is not instance of class 
        // String 
        boolean b1 = fun(i, "java.lang.String"); 
    
        /* print true as i is also instance of class 
           Number as Integer class extends Number 
           class*/
        boolean b2 = fun(i, "java.lang.Number"); 
    
        System.out.println(b); 
        System.out.println(b1); 
        System.out.println(b2); 
    

chevron_right


Output:



true
false
true

NOTE: instanceof operator throws compile time error(Incompatible conditional operand types) if we check object with other classes which it doesn’t instantiate.

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Test
{
    public static void main(String[] args)
    {
        Integer i = new Integer(5);
  
        // Below line causes compile time error:-
        // Incompatible conditional operand types
        // Integer and String
        System.out.println(i instanceof String);
    }
}

chevron_right


Output :

13: error: incompatible types: Integer cannot be converted to String
        System.out.println(i instanceof String);
                           ^

Related Articles:
new operator vs newInstance() method in Java
Reflections in Java

This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Improved By : _tanyajain

Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.