Java 10 Preview: Local-Variable Type Inference (var)!

Per the rolling release cycle promise, JDK 10 is schedule to be released on March 20th, 2018.
While it is not expected to be a major feature release, so far, it includes a feature that I find invaluable for clearing up java verbosity: Local-Variable Type Inference!
Now we can have code like this! :
var x = new HashMap<String,Integer>();
//inferred to be HashMap<String,Integer>
Instead of this….
HashMap<String,Integer> x = new HashMap<String,Integer> ();
Experienced readers will quickly see how this will become useful for shortening the syntactic baggage that has been carried along in Java since it’s inception. It doesn’t really serve the language much (in my opinion) to mandate typing the return type on the LHS if we can readily and accurately infer the type from the RHS. It makes for redundant code, which makes for less developer productivity.
The trade-off of this is that this would make Java code a little less strongly typed. It is worth the trade-off, in my opinion.
While this is great for situations where a lot of lasagna code in object instantiation could be avoided, it is perhaps not so good for assigning variables to the result of method calls.
Imagine you are working with several methods from various classes (as is the almost ubiquitous case). Is it clear where this call is coming from or what it’s return type is?
public void doGet() { ... }
public void doPost() { ... }
var r = doMath(); 
//No clue what type this returns!
//Where did it come from? Not immediately obvious to second hand readers of your code in large projects.
Nope. Also, how will this mesh with existing Type Inference in Java?
Type Inference with the Diamond Operator
One may think that type inference with the diamond operator cannot be used with local variable type inference but this is not the case. For example
Map<String,Integer> x = new HashMap <> ();
// Note that we do not need to repeat the paramater arguments
// in the RHS diamond operator as it is induced from the
// RHS return type.
var x = new HashMap<> ();
In summa, and in my opinion, var typing can be a good choice in reducing boilerplate when performing object initialization, working with generics, or working with static methods where the enclosing class is explicitly stated (Utilities.doX()).
For other situations, such as assigning a variable to the result of a method call (var x = someMeth()), var typing is not desirable, at least in my opinion.
Java was explicitly designed to be a strongly typed language where it would be as clear as it can possible be to users of code what the type of a member is.
While this weakens that to a certain degree, it is, again, worth it in my opinion, in the effort to reduce Java verbosity and developer efficiency.

Some Restrictions

1) Use falling outside of these categories
In Java 10, one would only be able to use Local-Variable Type Inference (var) with: 1) local variables with initializers, 2) for-loop indexes, 3) and within the local scope of a conditional loop. [1]
Notice something about this. var can be used with local variable initialization only! Which means you cannot declare a variable as follows:
var x; //No!!! -- Terrible code anyway!
Notice also that this means you cannot use var with Methods, Formals, Fields, or anything else other than the aforementioned. Perhaps Java will add support for var in an upcoming release. Maybe they won’t, time will tell. Either way, they will not be legal Java 10 code.
2) You cannot instantiate multiple variables either
var x=0,y=0; // no!
3) No Lambdas, Method References, or Array Initializers
4) No null values!
var x = null; //No!
var y = funcReturningNull(); //No! 
// We cannot infer the type.
5) You cannot reassign to a different type
var x=0; //Inferred to be of type 'int'
var x="c"; //No! - incompatible types!

Some Other Things to Know

  • Capture variables used with var are mapped to their supertypes. Any type arguments, if specified are replaced with bounded wildcards.
  • Some Non-Denotable types (like Anonymous Classes) can be inferred by var.

What is var itself?

One might be tempted to believe that var is a keyword. This is in fact, not true. var in Java will a reserved type name. After all, when we type var we are typing it where a type identifier would otherwise be.
The advantage of this is that code that already uses var as the identifier of some member or package won’t be affected because var is not a keyword.


Popular Posts