The code below compiles without error... for once I would have preferred it to fail :/
Map <Character, Double> m = new HashMap <Character, Double>();
m.get(new String());
Since the compiler knows that the key used in this map is of type Character, using a String key instead should be flagged as incorrect.
What I am missing ?
You're not missing anything. All Map#get()
calls simply take Object
.
Depending on the implementation, you might see a (runtime) ClassCastException
when you pass a String
to a Map<Character, Double>#get()
.
Here's why Map#get()
isn't fully generic.
You're missing an (optional) run-time exception (ClassCastException
), if you try running this code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With