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.
From the Java Language Specification 3.0, section 15.20.2:
Since you can’t compile a cast from a
HashMapto aDate, you can’t compile aninstanceoftest between the two either.