Java 10: Let’s play with var

Oracle is finally up to a time-bound release of java and announced the general availability of Java 10 on March 20, 2018, as part of the new six-month release cycle.

This release introduced twelve new enhancements defined by JEPS(JDK Enhancements Proposals). One of them is Local-Variable Type Inference (JEP 286).

In this release, var is introduced, which allows the compiler to infer the type of a local variable with its initializers. This feature was very common in many other languages like Swift, Scala, Go, C# etc.

An important thing to keep in mind about var of Java 10 is that it is not a keyword but just a reserved type name. A reason behind it could be to avoid any adverse effect on already written code which may contain var as any variable, method, or package name. It also means developers can still use var as a variable, method, or package name.

In Java, this feature is only restricted to local variables with initializers, indexes in enhanced for-loop, and locals declared in traditional for-loop.

Let’s see some examples where var could help developers.

In the above example, you may not be able to see the benefits of var keyword.

Now, let’s see a case where we want a list which could store different type of data.

For instance, we want to create a list which contains data of different data types like List.of(1,”Java 10”,12.3). In such a case, how will a developer decide the data type of the List?

A solution is var. Using var, the compiler will automatically convert the list in java.util.ImmutableCollections class. Let’s give it a try.

Isn’t it amazing? There could be many such cases.

Now, let’s see the usage of var with traditional for-loop.

var can be used with enhanced for-loop also. Code snippet given below can be referred.

Restrictions with var:

So far, We have talked about where var could help developers.
But it is not available for method/constructor/catch formals, method return types, instance variables, or any other variable declaration.

    1. Java’s var cannot be used for variable declaration. The compiler looks for an initializer in which it will infer the type of the variable.

 

    1. It cannot be used with method parameters and method return type. The reason is that the compiler would not be able to infer with which type var will be replaced at the runtime.

 

    1. It is not allowed for instance variables. Generally, we don’t initialize the instance variable at the time of declaration but var is restricted to be initialized and also developers use the constructor or getter-setter methods to read or initialize/change the value of instance variable.

 

    1. It is not allowed for class variables.

 

    1. It cannot be initialized with a null value. Null value doesn’t belong to any particular data type so it cannot help the compiler to infer the data type of the variable.

 

  1. Variable created using var cannot be re-assigned with a different data type. The data type of the variable initialized using var is inferred according to the data type of its initializer at the time of compile time only and cannot be changed.

We have seen few examples where we could and we could not use var. We can conclude with its following limitations.

  • Compromised with code readability.
  • var is very restricted with its usability and so the developer would have to think that where he/she could use it and where not.

I hope Java will work upon var more and will come up with a less restricted feature.

Hope you liked the blog. Thanks for reading!


knoldus-advt-sticker

References:
blogs.oracle.com
openjdk.java.net/jeps/286
dzone.com


Written by 

Nancy jain is a software consultant with experience of more than 6 months. She likes to explore new technologies and trends in the IT world. Her hobbies include watching web series, writing and travelling. Nancy is familiar with programming languages such as Java, Scala, C, C++, HTML, Javascript and she is currently working on reactive technologies like Scala, DynamoDb, AkkaHttp.

1 thought on “Java 10: Let’s play with var

Leave a Reply

%d bloggers like this: