Instanceof operator in java to compare different classes

I tried to see how the instanceof operator works in Java, and is facing a very strange problem.

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. Nevertheless,

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

It 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.

+8
java instanceof
source share
4 answers

From the Java 3.0 Language Specification, Section 15.20.2:

If casting the RelationalExpression expression to ReferenceType is rejected as a compile-time error, then the instance of the relational expression also generates a compile-time error. In such a situation, the result of an instance of an expression can never be true.

Since you cannot compile a listing from HashMap to Date , you cannot compile an instanceof test between them.

+9
source share

You declare Map m and use instanceof , because Map is an interface , which is a new class, can extends Date implements Map . Another (not Date) abstract class or class causes a compilation error.

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

@Bon Espresso almost said the correct answer, in my opinion. And here is my little addition to it:

You need to understand what exactly it actually does, so the definition:

instanceof is a binary statement that checks if an instance has a specific type

So when you do this:

  Map m = new HashMap(); 

At the time of compilation, m is actually an interface , but the actual test against instanceof occurs at run time with an NOT interface instance , so the compiler cannot be sure if the instance is behind m (some class that implements this interface) is actually a class that can extend date.

I mean, you might have a class with this declaration:

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

And you could do this:

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

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

The idea here, as you can see, is that if it is likely that validation with the instance will succeed, the compiler will not complain.

On the other hand, in the second example:

  HashMap m = new HashMap(); 

You declare the actual implementation of the map interface or instance . In this case, the compiler is sure that the HashMap is not expanding the date, which gives you an error.

+2
source share

Please refer to the JLS specification mentioned here http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2 .

Instanceof answered very well here - incompatible conditional operand types

0
source share

All Articles