Here's a simple class that does not use raw types, and it seems to me it should not compile, but it does:
import java.util.HashMap; import java.util.Map;
public final class MapBug {
public static void main(String[] args) {
Map<Integer, MapBug> integerMap = makeMap();
String txt = "Bad key -- wrong type!";
MapBug mapBug = integerMap.get(txt); // <-- No compiler error here?
}
private static Map<Integer, MapBug> makeMap() { return new HashMap<>(); }
}
Usually when I see this kind of problem, it's because the user is using raw types, but this code doesn't do that. The map is declared with Integer as its key type, and I'm clearly passing in a String. As far as I can remember, this used to produce compiler errors. What am I missing?
(I'm compiling with Java 1.8.0_31. I can't upgrade on my current system, but this wrong type should have produced a compiler bug way back in Java 1.5.)