var keyword in Java
- Details
- Written by Nam Ha Minh
- Last Updated on 25 August 2019   |   Print Email
This article helps you understand what is var keyword in Java and how to use it.
You know, var keyword is added to the Java language since Java 10 (JDK 10), supporting a new feature called local variable type inference, in which Java compiler guesses the type of the variables based on the surround context – allowing programmers to not declare the type explicitly.
Consider the following statement that declares a local variable:
List<String> list = new ArrayList<String>();
As you can see, in this variable declaration, the type information (List and String) is repeated two times, both on the left side and right side, which causes repetition and boilerplate code.
So it can be rewritten using the var keyword like this:
var list = new ArrayList<String>();
This may not be obvious to see the benefit of using the var keyword. Let’s consider another example:
List<String> list = Arrays.asList("One", "Two", "Three", "Four", "Five");
You can rewrite this statement using var like this:
var list = Arrays.asList("One", "Two", "Three", "Four", "Five");
Here, the Java compiler can infer the type of the variable list because the return type of the method.
Then you can use the inferred variable:
String first = list.get(0);
Take another example. Consider the following method:
public Map<String, List<Student>> foo() { // return a map }
Without using var, you have to specify the type explicitly when calling the foo() method like this:
Map<String, List<Student>> map = foo();
But using var, thing gets pretty much easier:
var map = foo();
So you see, using var keyword makes the code succinct, more readable and reduces boilerplate. However, there are some restrictions as well:
- You can use var only for local variables (in methods). It cannot be used for instance variables (at class level).
- You cannot use var in Lambda expressions.
- You cannot use var for method signatures (in return types and parameters).
And remember, you cannot use var to declare a variable without explicit initialization, hence the following:
var x;
is not allowed, since local variable declaration requires initialization on the right side. That also means this declaration is not valid:
var x = null;
See all keywords in Java.
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- 10 Java Core Best Practices Every Java Programmer Should Know
- Understand Interfaces in Java
- Understand abstraction in Java
- Understand encapsulation in Java
- Understand inheritance in Java
- Understand polymorphism in Java
Comments
Could you please explain this? Thank you so much.
The var keyword is a shortcut, thus it comes with a side effect as you mentioned.
Apart from that, if you're somewhere further in your program, you might not even know anymore what real type you're dealing with, unless you go back to the assignment place. I can only hope that the IDEs are intelligent enough to help you with that if you hoover over the variable.