Everything you need to know about Local Variable Type Inference: Java10

Table of contents
Reading Time: 4 minutes

In this article, I will be talking about Local variable type inference in Java. Local variable type inference in Java was released as part of Java 10 and it was the highlight feature of Java10.
Now, we’re going to look at the biggest change in Java 10 which was the introduction of local variable type inference. The Java language is sometimes considered to be a bit verbose. The language change we’re going to look at is an attempt to reduce the amount of boilerplate code we have to type in Java.

What is Local variable type Inference?
This feature is all about the variables in Java, whenever we declare a variable on the left-hand side we define a data type with the name of the variable and on the right-hand side, we define the expression to initialize the variable. Since we are talking about local variables here, these variables are limited to method scope i.e we are talking about variables inside the methods.

Local variable type inference- The type for the local variables i.e variables declared inside the method will be inferred automatically by the compiler. We do not have to tell the compiler about the type for a variable, it will be inferred by the compiler itself.

For example
Before Java 10 – String name = “Deepak”;
After Java10 – var name = “Deepak”;

As we can see in the example above the type of the variable is not mentioned, we have just used var to declare the variable and it will compile just fine. Now, the compiler will automatically infer the data type for the variable by taking the expression on the right-hand side in the account, since the expression is String, the compiler will infer it and assign the type accordingly.

This feature is absolutely not a unique feature, we have seen a similar feature in languages like C#, Scala, or Kotlin.

Why do we need Local variable type inference?

1. Type less code.
2. Makes things simple.
3. Clean code.

For example –

If you look at the example above, it is clearly visible that after using var the code looks much cleaner and one can focus more on the variable names instead of just struggling with the data types.

When it can go wrong?

You cannot just overuse or abuse var just for the sake of simplicity and reducing the amount of code you write, consider the code below to understand it better.

It is said that the code is written only once and read many times. In the example above, it won’t be wise to use var as first of all, the method is not named very well. Also, it will be difficult here to determine the type of variable result. So, instead of just using the var here, we can probably give it type if can really not fix the method name. This code will be read by many developers during code reviews and others, so it is better to not use vars here.

Some facts about var

Var is not a keyword – Var is not a keyword, as introducing a new keyword in Java is really a big change. Also, keywords in Java cannot be used as identifiers. Var is a reserved type, not a keyword. So, it is okay to use the following statement.

Java turned into a dynamically typed language – No, even with this change we cannot say that Java turned into dynamically typed language as the compiler will just infer the type using the right-hand side expression. However, internally it will actually assign the type. Let’s consider an example, where we will use a piece of code with and without var and see the compiled version to check if there is a difference between these two.

Here are the results
// Result without using var

// Result using var

If you look at the result, both the compiled versions are identical and there is no change at all. This proves that internally the type is assigned as usual.

Is Type inference new in Java?
The answer is no, the type inference is not new, we have seen this before as well
Before java 7, we used to provide types explicitly

Java7 onwards

The only difference is, type inference will happen from left to right.

Limitations of Type Inference
1. Lambdas must have an explicit target type – As there will be multiple functional interfaces that take input A and return B and in this case the compiler will get confused and will not be able to determine the type.

2. Var in combination with diamond operator – Consider the example below
Var list = new ArrayList<>();
In this example, the statement will compile. However, the type would be Object. To continue using the var with a specific type, use the type with a diamond operator like below.

Var list = new ArrayList();
3. Local type inference only – Only applicable to local variables inside the methods.

4. Var won’t work without initializer – If you are using var without initializing the variable, it won’t work and give you errors.

Var name; // won’t work
Var name = “Deepak”; // works fine

5. Var won’t work with array initializer – If you are using vars with array initializer like below, it won’t work

Var array = {1, 2, 3, 4}; // would produce error.

Conclusion – Var is great to be used when you want to avoid types with long names and also wants to focus on the variable names instead. However, you will have to be very careful and use it wisely.

That’s pretty much it from the article. If you have any feedback or queries, please do let me know in the comments. Also, if you liked the article, please give it a thumbs up and I will keep writing blogs like this for you in the future as well. Keep reading and Keep coding 🙂


Knoldus-blog-footer-image

Written by 

Deepak is a Software Consultant having experince of more than 5 years . He is very enthusiastic towards his work and is a good team player. He has sound knowledge of different technologies which include Java, C++, C, HTML, CSS, Javascript, C# always keen to learn new technologies.