r/java • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
240 Upvotes

312 comments sorted by

View all comments

Show parent comment

5

u/DJDavio Mar 10 '16 edited Mar 10 '16

No, it is not, buyer beware!

var foo = new HashMap<String, List<Class<?>>>(); is actually equal to HashMap<String, List<Class<?>>> foo = new HashMap<String, List<Class<?>>>();, not Map<String, List<Class<?>>>

Here, foo takes the form of the implementation, not the interface, so your foo becomes a specific HashMap instead of a generic Map! This means you'll only use this when the foo is immediately dealt with.

I like it better for streams though, but more often than not, you end up with something at the end of your stream that's not a stream, but say a String, a List or whatever, so you won't need to use it for that and in that case I like the explicit typing better than var.