Why is it possible to compare incompatible types by reference in Java?
comparison, java, pointers, types
Solution
It's permitted specifically because `List` and `Map` are interfaces.
We could imagine some class
// (please only imagine)
class ListMap implements List, Map {...}
Compile-time legality of reference equality (15.21.3) is the same as that of reference type casting (5.5.1). In short, since you can generally cast between any reference type and an interface, you can also generally compare reference equality of any type to an interface.
The permission seems more useful in the context of smaller interfaces like `Comparable`, `Serializable`, `Iterable`, etc., where a class is more likely to implement more than one.
Problem
Check out this snippet: ``` List<Integer> c = new ArrayList<>(); Map<String,Boolean> m = new HashMap<>(); if( c == m ) //no error here! WHY? { c = m; //"Incompatible types" error, as expected. m = c; //"Incompatible types" error, as expected. } ``` How come `c == m` gives no error? I am using the javac of jdk1.8.0.20 and I have no reason to suspect that it disregards the java language specification, so this is with a fairly absolute level of certainty in the spec, so: What is the point / purpose / usefulness of having something like this allowed by the spec?