[Java] To var, or not to var, that is the question

Victor Hugo
4 min readDec 4, 2022

--

Long time no see! Today we’re gonna talk about something that is not new but at the same time it still makes people unsure. We gonna talk about var in Java.

What is var about?

Let’s first understand what is var, var is a way to enable type inference to declarations of local variables with initializers. Too technical? Here is an example:

// traditional variable declaration in Java
List<String> names = List.of("Victor");
for (String name : names) {
// ...
}


// variable declaration with type inference
var names = List.of("Victor");
for (var name : names) {
// ...
}

// we can even declare constants
final var name = "Victor";

First thing you can notice is that with var we don’t need to explicitly declare the type, it will be inferred by the compiler by looking at what was passed as the initializer in the declaration. That means we can’t set it as null because all non-primitive types can assume a null value, so there’s no way the compiler knows what type should be assigned to the variable. Another thing is that var works just like a short-hand for the traditional variable declaration, so once it is declared, we can’t assign a new value of a different type.

var name = null; // Compilation failure cannot infer type for local variable name

var userName = "victorhsr";
userName = 2; // Compilation failure incompatible types: int cannot be converted to java.lang.String

Another technical detail, var is a reserved type name, not a keyword. We can still use it as a class name and also as the name of our variables.

class Var{}

public static Var var() {
var var = new Var();
return var;
}

A little of history

The idea to add var to the Java Language appeared around 2016 by the team of the OpenJDK. They officialized the proposal and submitted it as a JDK Enhancement Proposal (JEP) known as JEP-286.

The Java community started to speculate about it, many people thought it was a good idea and some don’t. To decide what to do and also to bring more representativity to the decision, a survey was made collecting the feelings and wishes of the community.

As you can see the idea was well accepted, of course a lot of discussion happened in this period and finally, in 2018 the JEP-286 was included in the release of the JDK 10.

Discussion

As everything in software engineering (and in life in general) there are pros and cons. In the next topics I’m listing some of the characteristics of the usage of var raised by the community and finally, a verdict with my personal opinion which is aligned with the one explicited by the OpenJDK team.

Pros

  • Speed when typing;
  • Less verbosity;
  • Useful to simplify complex expressions;
  • It encourages developers to write more descriptive variable names;

Cons

  • It makes the readability worse, specially outside the IDE, for example in GitHub;

Verdict

I’m a fan of type inference, languages like Kotlin and TypeScript also have this functionality and both work pretty similar to Java’s var. In projects with those languages we don’t face the same problem pointed out by the community in the Cons list… But why’s that?

Many Java developers are used to just relying on the language’s type mechanism and not in the expressiveness of their code.

We must get used to writing more descriptive names for variables, methods, and classes. If we write readable code with descriptive names, the type of the variable won’t matter so much. Here is an example written by the OpenJDK team:

UserModelHandle userDB = broker.findUserDB();
List<User> users = db.getUsers();
Map<User, Address> addressesByUser = db.getAddresses();

Each variable has a role in our code, and that’s what matters. In this case, I must say that the type definition is making the code less readable, once the name of the variables is not so easy to visually pick out from the code.

var userDB = broker.findUserDB();
var users = db.getUsers();
var addressesByUser = db.getAddresses();

The above code is using var, it makes it easy to see and understand the variables. But to work, we need to give good descriptive names to our variables, and that is a good practice for all languages.

In our IDE we can still see the type of var variables but not in Git repositories. This can reduce readability for sure, BUT this is even more an opportunity to improve our codebase. If you find it difficult to understand what is happening in a pull request just because it is using var, thats a strong indicator that there is a problem with the naming used, so you and your team can work around to improve it.

Remember that in Java we are already used to type inference, we use it a lot in the Streams API, var is just making a wider use of this well known technique.

Type inference in variable declaration is something broadly used in other languages and it happily came to Java. Give it a try, use that as an opportunity to improve your coding skills!

If you enjoyed the article, press the clap button 👏 to encourage the author and make the article more accessible to the audience

--

--