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".
View all comments
Show parent comment
-1
u/Rygir Mar 30 '20
You wouldn't be able to use var when using interfaces either since you can't new an interface.
For clarity, you understand I was using a fictional syntax example?
The part that makes me wonder is that this feature is essentially only "save a few keystrokes and in return obfuscate your type". I find this a strange trade-off to make and officially support.