Simplify Local Variable Type Definition Using the Java 10 var Keyword

As many of you might have heard, Java 10 was released in March 2018. It is a short-term release from Oracle Corporation and came with lot of new features and enhancements. One of the important features in Java 10 is local variable type inference, which is detailed in JEP (Java Enhancement Proposal) 286. The upcoming Java release, due in September 2018, will be a long-term-support (LTS) version of Java. (Note that generally, LTS releases are due every three years.)

Let’s look at a  Java 10 local variable type inference feature example now.

The main advantage of this feature is to reduce boilerplate variable type definitions and to increase code readability. Here’s an example:

String s=new String("Java 10");
Integer int=new Integer(10);

A Java developer would have no problem reading the above two statements. However, as another example, here are some more-complex statements that are kind of pain to write:

MAP<String,String> map=new HashMap<String,String>(); 
MAP<User,List<String>> listofMovies=new HashMap<>();

In Java 10, the var keyword allows local variable type inference, which means the type for the local variable will be inferred by the compiler, so you don’t need to declare that. Hence, you can replace the above two statements as shown below:

var map=new HashMap<String,String>();
var listofMovies=new HashMap<User,List<String>>();

Below are the few points to remember about local variable type inference in Java 10:

1. Each statement containing the var keyword has a static type which is the declared type of value. This means that assigning a value of a different type will always fail. Hence, Java is still a statically typed language (unlike JavaScript), and there should be enough information to infer the type of a local variable. If that is not there, compilation fails, for example:

var id=0;// At this moment, compiler interprets 
//variable id as integer.
id="34"; // This will result in compilation error 
//because of incompatible types: java.lang.String 
//can't be converted to int.

Notice that JavaScript also has the concept of a var keyword, but that is completely different from Java 10 var. JavaScript does not have type definitions for variables. As a result, the above example would have been successfully interpreted by the JavaScript runtime, and that is one of the reasons TypeScript was introduced.

2. Let’s look at an inheritance scenario. Assume there are two subclasses (Doctor, Engineer) extended from the parent class Person. Let’s say someone creates an object of Doctor, as shown below:

var p=new Doctor(); // In this case, what should be
//the type of p; it is Doctor or Person?

Note that in such cases, a variable declared with var is always the type of the initializer (Doctor, in this case), and var may not be used when there is no initializer. Therefore, if you reassign the above variable p, as shown below, compilation fails:

p=new Engineer(); // Compilation error saying
//incompatible types

So we can say that polymorphic behavior does not work with the var keyword.

3. The following  are places where you cannot use local variable type inference:

a)  You can’t use local variable type inference with method arguments:

public long countNumberofFiles(var fileList);// Compilation 
//error because compiler cannot infer type of local
//variable fileList; cannot use 'var' on variable without 

b) You cannot initialize a var variable to null. By assigning null, it is not clear what the type should be, since in Java, any object reference can be null. In the following example, because there is no predefined data type for a null value, the compiler is not able to interpret a type for count, which would cause a complication error.

var count=null;// Compilation error because 
//compiler cannot infer type for local variable
//count since any Java object reference can be null

Note that JavaScript has a data type NULL, which can hold only one value: Null.

c) You can’t use local variable type inference with lambda expressions, because those require an explicit target type. For example, the following causes a compilation error:

var z = () -> {} // Compilation error because
//compiler cannot infer type for local variable z;
//lambda expression needs an explicit target type

Java 10 var is desgined to improve the readability of code for other developers who read the  code. In some situations, it can be good to use var; however, in other situations it can reduce the readability of code.

Here’s an example of looping over an entrySet of a Map:

Map<String, List<String>> companyToEmployees= new HashMap<>();
  for (Map.Entry<String, List<String>> entry: companyToEmployees . entrySet()) {
      List<String> employees= entry.getValue();

Let’s rewrite the above code using Java 10 var:

var companyToEmployees= new HashMap<String, List<String>>();
  for (var entry: companyToEmployees. entrySet()) {
       var employees= entry.getValue();

From the above example, it is clear that using var might not always be good.

For more information about the var keyword, I recommend going through the Java 10 local variable type reference docs.