Hacker News new | ask | show | jobs
by kinghajj 3030 days ago
Yes, but in current Java one would have to write

ArrayList<String> foo = new ArrayList<String>()

Or, at least

List<String> foo = new ArrayList<String>()

Whereas with `var`, as you show, the compiler infers the type. Proponents of this point out exactly what you did: the type is right there, so why should the programmer have to write it twice?

2 comments

Because nobody actually writes this twice.

n,<return>,A,r,L,<return>,S,t,r,<return>,Cmd-Option-V,<return>

The IDE autocompletes everything, extract-to-variable does most of the heavy lifting.

> List<String> foo = new ArrayList<String>()

List<String> foo = new ArrayList<>()

> so why should the programmer have to write it twice?

You wouldn't.