3 ms·
As viraptor said, it's always safe to just use the most restrictive type. Type inference is quite useless if you just infer everything to be an Object :) And
by dschobel 17y ago
As viraptor said, it's always safe to just use the most restrictive type. Type inference is quite useless if you just infer everything to be an Object :)
And beside, in cases of ambiguity, you can mandate the use of the verbose form.
I was thinking specifically of the example in C# where you can do something like:
var list = new[] { "hello", null, "world" }
which the runtime casts as a string[].
It just seemed like these collection literals would be good candidates to use the new type-inference capabilities they're rolling out.
- gchpaco 17y agoSpecifically because of Java's wildcarding, there is an important difference between a Map<Object, Object> and a Map<? extends Object, Object>; a Map<String, Object> is not the former and is an example of the latter. This comes up in method definitions quite frequently. Yes, this is a really stupid feature of the language.
- WilliamLP 17y agoThis one is of course a circular debate that will live forever, but you might expect to be able to insert an Integer key into a Map<Object,Object>, which you can't for a Map<String, Object>, so to say the second "is" an instance of the first doesn't really work.
- gchpaco 17y agoYou are, of course, quite right, which is why I said that a Map<String, Object> isn't a Map<Object, Object>. However in Java's typing scheme it is a Map<? extends Object, Object>, which represents a key that extends Object (very specific, that) and among other things won't let you insert anything into it.