DEV Community

How to use the var keyword in Java? Example Tutorial

javinpaul on April 02, 2019

Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided i...
Collapse
 
skuzzle profile image
Simon Taddiken

It's worth mentioning that var is not intended to replace all your explicitly typed local variable declarations. It need to be applied with caution in order to not screw up the readability of your code.
For instance, the example above with the OutputStream declaration is not really chosen carefully. With var you are forced to put more emphasis on the naming of your variables. The statement from the example above could have looked like this:

var bos = processFile();
Enter fullscreen mode Exit fullscreen mode

Here you have no clue about what bos might be. Using acronyms like bos is a bad practice anyway but may become painful when using var. Use proper naming like this:

var outputStream = processFile();
Enter fullscreen mode Exit fullscreen mode

Also it might not be the best to compare Java's var with JavaScript's var as the latter is typed entirely dynamical.

Collapse
 
abstratt profile image
Rafael Chaves

I'd question the practice of including the type name in the variable name. Good names denote role or purpose, not type.

In your example, it would matter what the data in the file is going to be used for (say, contentsToCompress, imageContents, dataToSort etc).

Collapse
 
javinpaul profile image
javinpaul

I completely agree with you that naming becomes even more important with var now. A variable name like "bos" does make sense when you have ByteArrayOutputStream as type but with var, yes, we got to be more careful. Thanks for highlighting that part.

Collapse
 
lluismf profile image
Lluís Josep Martínez

Many things which take just 5 minutes in languages like Python, Groovy, or JavaScript can take more than 30 minutes in Java due to its verbosity. This is such a bullshit. Variable declaration is a very small part of a class/method. Let's say a 5% of the total. If not declaring the type removes 50% of the declaration, the total gain is about 2%. Nothing to fuss about!

Collapse
 
javinpaul profile image
javinpaul

That's true and that's why Java is not for writing scripts or quick utilities but at the same time, I have found managing a large and super large project in Java much easier than let's say Python or JavaScript. Though I am not a Python or JavaScript expert, I have just happened to work on that, but that's what I have felt during my slightly limited exposure.

Collapse
 
diegopm profile image
Diego Pardo

lol

Collapse
 
ryselis profile image
Karolis Ryselis

Java var is nothing like Python's def. def is used to declare a function or method.

Collapse
 
javinpaul profile image
javinpaul

Yeah, that's true, Java var is less powerful than Python def or even JavaScript var but it still useful on making your code elegant and concise.

Collapse
 
milan997 profile image
milan997

Haven't really understood it in Java or C#, I don't understand the gains except for not having to type a few characters, and you're loosing that nice readability in statically typed languages.

Also, var should be illegal now in javascript, having let and const.

Collapse
 
javinpaul profile image
javinpaul

It's not just typing but readability improves with more concise code but yes, you got to be more careful with variable names now because you just have one identifier to use to convey your intention and what the variable is and what it does.

Collapse
 
javinpaul profile image
javinpaul

Indeed :-)

Collapse
 
diegopm profile image
Diego Pardo

Java does NOT have dynamic typing features, before or after java 10. var still is static typing.

Collapse
 
lluismf profile image
Lluís Josep Martínez

Not really.