Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instanceof operator in java for comparing different classes

I was trying to see how instanceof operator in Java works and am facing a very odd issue.

public static void main(String[] args) {
    Map m = new HashMap();
    System.out.println("m instanceof Date: " + (m instanceof Date));
}

The above returns false as expected. However,

public static void main(String[] args) {
    HashMap m = new HashMap();
    System.out.println("m instanceof Date: " + (m instanceof Date));
}

This does not even compile. I get an error

inconvertible types
found   : java.util.HashMap
required : java.util.Date

What am I missing here? I am using IntelliJ Idea 11.

like image 651
Ankit Dhingra Avatar asked Feb 02 '12 06:02

Ankit Dhingra


People also ask

Does Instanceof work for subclasses?

As the name suggests, instanceof in Java is used to check if the specified object is an instance of a class, subclass, or interface. It is also referred to as the comparison operator because of its feature of comparing the type with the instance.

How do you check if a class is an instance of another class?

The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .

What is the Instanceof operator in Java is?

instanceof is a binary operator we use to test if an object is of a given type. The result of the operation is either true or false. It's also known as a type comparison operator because it compares the instance with the type. Before casting an unknown object, the instanceof check should always be used.

What is Instanceof a class?

instanceof is a keyword that is used for checking if a reference variable is containing a given type of object reference or not.


1 Answers

@Bon Espresso almost did say the right answer in my opinion. And here is my small adding to it:

You have to understand what instanceof really does, thus the definition:

instanceof is a binary operator that checks if an instance is of a certain type

So, when you do this:

 Map m = new HashMap();

At compile-time m is actually an interface, but the actual test against instanceof happens at runtime with an instance NOT an interface, thus the compiler can not be sure if the instance "behind" m (some class that implements this interface) is actually a class that could extend Date.

I mean you could have a class with a Declaration like this:

class MyClass extends Date implements Map{
     .....
}

And you could then do this:

 Map myMap = new MyClass();
 (myMap instanceof Date) 

and it would be perfectly legal, because MyClass actually extends Date.

The idea here as you can see, that if there is the smallest chance that the check with instanceof will succeed, the compiler will not complain.

On the other hand in your second example:

    HashMap m = new HashMap();

You are declaring an actual implementation of the Map interface, or an instance. In this case the compiler is sure that HashMap does not extend Date, thus giving you the error.

like image 200
Eugene Avatar answered Nov 15 '22 20:11

Eugene