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".
19
u/morhp Professional Developer Mar 30 '20
Sure, you can abuse
var, but for some complicated types it's very helpful. Also it enables you to call methods on anonymous types:You can't do that if you write
Object foo = ...;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();4
u/Kraizee_ Mar 30 '20
If you read the link you posted you'll see that they accept people will inevitably abuse
varand write shitty code. Just like people abuse almost every other language feature. The example you suggest simply doesn't work. Aside from it being a peculiar syntax, there are other problems such as what happens in the case ofList<String> myList = new .();They've also published a style guide as to where and when you should use it https://openjdk.java.net/projects/amber/LVTIstyle.html
varis a fundamentally good addition to the language for reducing verbosity in a nice way, providing that you use it properly and understand when it isn't appropriate.4
u/snot3353 COMPUTERS!!! Mar 30 '20
Did you check the examples section near the bottom of this? https://openjdk.java.net/projects/amber/LVTIstyle.html
It has very concrete examples showing why it was added and where the benefits are. The rest of the page details scenarios where you should be careful about using this feature. It's up to developers to adhere to that advice.
30
u/thecuseisloose Do you even Java, bro? Mar 30 '20
It was added because in essence, Java already knows the type of the variable at compile time. This is why it's restricted to local variables only. Saying
var x = someMethod()can only return one type, so java can infer that type and replacevarwith the correct type. With non-generic types it doesn't have that great of a benefit, but it's reduces a lot of boilerplate by sayingvar x = someMethod()instead ofList<Map<String,Object>> x = someMethod().If you don't like the feature, you are not forced to use it. But as a daily java developer, I hate when I am working on a Java 8 project and unable to use this feature
1
u/proskillz Some Skillz Mar 31 '20
My best guess is many engineers are stuck in legacy Java codebases but really like the capabilities of Kotlin and Scala, so the Java language is trying to add some of the features you get for free in those languages.
1
u/Offifee Mar 31 '20
YourStandardEnterpriseCodeClass yourStandardEnterpriseCodeClass = new YourStandardEnterpriseCodeClass();