Local Variable Type Inference: Declare Var, Not War
This tutorial teaches the basics of Java's local variable type inference, specifically where you can and can't use var in your code.
Join the DZone community and get the full member experience.Join For Free
Java is changing rapidly, and with the six-month release cycle, we are getting new features to try with every release. In Java 10, local variable type inference was added. It was basically aimed at reducing boilerplate code and improving readability when declaring local variables with initializers.
Programs must be written for people to read and only incidentally for machines to execute
Let's understand this with an example. The following code is written without using local variable type inference:
After refactoring as per Java 10 local variable type inference:
In the refactored code, the compiler can infer the declared type itself by looking at the RHS declaration.
These are just some of the examples to help you understand the feature and how we can use local variable type inference.
Now, let's understand where local variable type inference can and cannot be used.
Where it can be used:
- Local variable initializers
- Indexes in an enhanced for-loop
- Locals declared in a traditional for-loop
- Resource variables of the try-with-resources statement
- Formal parameters of implicitly typed lambda expressions. (Support added in Java 11)
The following code snippet shows some valid examples:
Where it cannot be used:
- Method parameters
- Method return types
- Local variable declarations without any initialization
- Cannot use null for initialization
In the following code snippet, I have explained which compiler errors will appear if local variable type inference is used in an unsupported way.
What's the Benefit?
If you ask me personally, I think developers should use it judiciously. There is excitement whenever a feature comes in, and you definitely want to give it try. But, you have to understand that we, as developers, read code more often than we write it.
Since this is related to readability, some folks will like it and some will hate it. So, if during a code review, someone says that they are not able to get the declared type of a var, then it means that it is not very clear to others, so maybe switching back to the old-fashioned way where we declare types explicitly is not that bad after all. Again, in some situations, the declared type is quite obvious, so you can skip the explicitly declared type and make use of the var.
In this article, we have covered what Java 10's local variable type inference is with some examples where it can be used and where it's not supported. You can read these FAQs prepared by the OpenJDK team for more information.
If you like what you just read, then you can buy me a coffee by clicking the link in the image below:
You can also read some of my previous articles here.
Published at DZone with permission of Ashish Choudhary. See the original article here.
Opinions expressed by DZone contributors are their own.