https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/var-in-java-keyword-type-inferred-benefits-examples-final-scope-rules
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
Developers and programmers have widely adopted the use of var in Java code because it provides a variety of practical benefits,:
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
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
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:
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.
Cameron McKenzie is an AWS Certified AI Practitioner, Machine Learning Engineer, Solutions Architect and author of many popular books in the software development and Cloud Computing space. His growing YouTube channel training devs in Java, Spring, AI and ML has well over 30,000 subscribers.
18 Jul 2025