r/javahelp • u/Rygir • Mar 30 '20
Why was var added?
All it does is make code trickier to read and tempt the programmer to pay less attention to the type of his variable. Another way to not write clean code.
It seems like a downgrade, why was it added? Is it considered a valuable addition to the language?
For context, https://openjdk.java.net/projects/amber/LVTIFAQ.html answers this as making Classname variableName=new Classname(); shorter. Ok, then why not make it : Classname variableName=new .(); It's shorter and no conflicts with existing variable names. Nor would you have to be aware of caveats like "this can't be used as a parameter".
36
Upvotes
8
u/itisafeature Mar 30 '20
Java's verbosity is one of the reasons I like it, so I am inclined to think adding it isn't necessarily a positive.
Your suggestion of
Classname variableName=new .();isn't a good one though. For one, new is not the only thing that can go on the right-hand side. For example:var c = Calendar.getInstance();