How to use Java’s var keyword
Packaged as part of the 2018 version 10 release, the Java var reserved word introduced type inference to the strongly typed Java programming language.
Type inference in Java happens when the JDK’s compiler, rather than the developer, assigns a data-type to a variable.
|Keyword vs. reserved words in Java|
To be completely accurate, var is a reserved word. var is not a Java keyword.
As you can see in the following var examples, the left-hand side of the assignment does not reference a Java types such as long, double or ArrayList. Instead, it uses the Java var keyword, which allows the JDK’s compiler to pick the appropriate type instead.
var x = 5.5; // double is inferred var y = System.currentTimeMillis(); // long is inferred var z = new ArrayList<Double>(); // ArrayList is inferred
Benefits of using var in Java
Developers and programmers have widely adopted the use of var in Java code because it provides the following benefits:
- A simplified syntax for method block logic.
- Easier onboarding experience for developers new to Java.
- Enhanced ease of development for methods and local blocks of code.
Inferred typing with var examples
When you use a var to declare a variable, the JVM assigns a type to the variable based on what it sees on the right-hand side of the assignment operation. That’s why the var reserved word is said to support inferred typing. The Java compiler looks at the manner in which the variable is initialized, and logically infers the type from it.
In the following var examples, it’s not difficult for the reader to logically infer the type of each variable.
Furthermore, the contrast between the explicit type declaration and the use of the var reserved word demonstrates how you can use the var reserved word to create simplified and less verbose code.
String mister = "dressup"; var bean = "lima"; // var infers String type int x = 10; var y = 20; // var infers int type LocalDate today = LocalDate.now(); var rightNow = LocalDate.now(); // var infers LocalDate type
Strong typing and the var keyword
Java’s var keyword reduces the verbosity and ceremony surrounding the declaration of local variables, but it does not sidestep any rules about the platform’s strong typing requirements. All rules about strong typing in Java remain in place.
For example, if Java infers a type to be a String, it will not assigned that type to an int or a float later on in the code.
int x = 10; x = "ten"; // Error: violation of strong typing in Java
When not to use var in Java
The var keyword cannot be used in place of every variable declaration in a Java program. Use of the var keyword is limited to a confined set of situations and scenarios. Limits and restrictions to the use of var in Java include the following:
- var cannot be used in a method signature.
- var cannot be used to declare instance variables.
- var cannot be used to declare static variables.
- var cannot be used if a variable is initially set to null.
The ongoing adoption of var
The introduction of the Java var reserved word was not a revolutionary change that impacted how the JVM behaved. Instead, it was more of a friendly change that makes Java methods easier to write, easier to read and more approachable for programmers who are new to the language.
Expect to see var appear more frequently in Java codebases in the near future. As organizations move away from Java 8 implementations and adopt Java 21, var will invariably become the norm.