The latter has a feature called type inference, which rose quite some discussion in the Java community. Wikipedia explains it as follows:
Type inference, or implicit typing, refers to the ability to deduce automatically the type of a value in a programming language. It is a feature present in some strongly statically typed languages. It is often characteristic of — but not limited to — functional programming languages in general. ... The ability to infer types automatically makes many programming tasks easier, leaving the programmer free to omit type annotations while maintaining some level of type safety. Explicitly converting to another data type is called "casting" (or a "cast").
After reading this definition, I immediately asked myself: Are we too lazy? All this does is save some typing? We have our IDEs which, in some sense, already infer types for us. For instance they suggest the correct parameters when calling a method, based on which type you need to pass. Doesn't type inference just sound like a "nice to have" feature of a programing language? Is it really valuable?
Ola Bini for instance claims that type inference could save some typing in Java, but probably not more:
Type inferencing would probably not do much more than save you some typing. But how much typing it would save you could definitely vary depending on the type of type inference you added.
Well, to be honest I am a little lazy these days. Anything that saves me some keystrokes in Java and still keeps my code readable and thus maintainable is very welcome. I don't expect fancy stuff. I just think something like the following, would be quite neat:
Map<Integer, String> keyVals = new HashMap<Integer,String>(); // duplicated type declaration
Map keyVals = new HashMap<Integer,String>(); // with type inference Map becomes Map<Integer, String>
Map<Integer, String> keyVals = new HashMap(); // with type inference HashMap becomes HashMap<Integer, String>
Map<? extends Number, String> keyVals = new HashMap<Integer,String>(); // if needed add generic type declaration
I'm not a language expert or compiler designer and I'm pretty sure I'm missing a lot of corner cases, but I guess implementing this kind of type inference should not be impossible. In fact, Ola Bini has a similar example in the aforementioned blog post.
The second statement of my example above, actually compiles in in Java today. You'll probably get a warning by your IDE for using a "raw type", but that's about it. The difference with type inference is that the compiler will check whether you use a correct typed key/value pair:
Map keyVals = new HashMap<Integer,String>();
keyVals.put("1", new Integer(4711)); // works today, with type inference compiler will know it won't
In a recent blog post Jeff Atwood wrote about how C# takes it even one step further. You can completely omit types on local variables and let the compiler do its magic. In Java it could look like this:
var keyVals = new HashMap<Integer,String>();
keyVals.put("1", new Integer(4711)); // compiler knows that this is not allowed
This would be nice, but I'm not even asking for that. I simply would love to see, that the compiler infers the generic type of key and value of a Map, so that I have the same type safety as with a generified Map in Java today. I think you would get the best of both worlds: static typing with less keystrokes.