Overview

Java 10 is on the board to make an entry into programming worlds. Local-Variable Type Inference is the most interesting feature in Java 10 out of other JEP (JDK Enhancement Proposals). The features targeted for this release can be found here.

Goals introduce Local-Variable Type is to simplify the writing of Java applications.

Nearly all other popular statically typed “curly-brace” languages, both on the JVM and off, already support some form of local-variable type inference: C++ (auto), C# (var), Scala (var/val), Go (declaration with :=). Java is nearly the only popular statically typed language that has not embraced local-variable type inference.

What is var?

var in Java 10 is a reserved type name. var is not a keyword.

Due to reserved type name, It will not affect to the existing identifier of any member or package in your application.

What is Local Variable Type Inference?

Previously, all local variable declarations required an explicit (manifest) type on the left-hand side. With type inference, the explicit type can be replaced by the reserved type name var for local variable declarations that have initializers. The type of the variable is inferred from the type of the initializer.

In other words, you’ll be able to declare variables without having to specify the associated type.

That will replace the current syntax:

As you can see, the Local Variable Type Inference will allow using the var instead of specifying the type of the variable.

We need to provide the generic type information on the RHS, otherwise it will result in an ArrayList of Object.

How to use Local Variable Type?

We have created multiple scenarios where it can be used. For easy execution, we are using JShell. If you do not know JShell. You can learn at JShell with Example and Useful Commands in JShell

  1. As a Local Variable
  2. As we can see at line number 1 and 4, we have created local variable using var.

  3. In a Static/Instance Initialization Block
  4. var can be used in a static block. Scope of the variable should be within static block. It won’t allow instance level declaration.

  5. As a Return Value in a Method
  6. As shown in the example, Scope of var is only for method. Here case should be taken care as with an explicit type on the left-hand side, the numeric value may be silently widened or narrowed to types other than int. With var, the value will be inferred as an int, which may be unintended.

  7. In a Collection with Diamond Operator
  8. In a Collection with Parameterized Collection
  9. As we see in the above example, It has allow collection with any type of collection. Here Map will be of type ImmutableCollections

Restrictions in Local Variable Type Inference

  1. It is not allowed as Instance Variable.
  2. Variable without initialization is not allowed.
  3. var x; //This is not legal!
  4. Multiple Variable instantiation is not allowed.
  5. var x = 0, y = 0; //This is not legal!
  6. Initialization with null values is not allowed.
  7. var x = null; //This is not legal!
  8. We cannot reassign to a different type.
  9. var x = “a”; //Inferred to be of the type of ‘String’ var x = 10; //Not allowed. Incompatible types
  10. It is not allowed as a parameter for constructor.
  11. public static var printNumber(){ }
  12. It is not allowed as method arguments.
  13. public static void sumOfNumber(var x, var y){ }
  14. It is not allowed in catch parameter.
  15. try{ //some operations }catch(var ex){ }
  16. It cannot be used as the name of a class or interface.

Conclusion

Using var for declarations can improve code by reducing clutter, thereby letting most important information stand out. On the other hand, applying var indiscriminately can make things worse.

Used properly, var can help improve good code, making it shorter and clearer without compromising understandability.

Leave a Reply

Your email address will not be published. Required fields are marked *