Local Variable Type Inference or LVTI in Java 10
Last Updated :
16 May, 2018
What is type inference?
Type inference refers to the automatic detection of the datatype of a variable, done generally at the compiler time.
What is Local Variable type inference?
Local variable type inference is a feature in Java 10 that allows the developer to skip the type declaration associated with local variables (those defined inside method definitions, initialization blocks, for-loops, and other blocks like if-else), and the type is inferred by the JDK. It will, then, be the job of the compiler to figure out the datatype of the variable.
Why has this feature been introduced?
Till Java 9, to define a local variables of class type, the following was the only correct syntax:
Class_name variable_name=new Class_name(arguments);
For example:
import java.util.ArrayList;
import java.util.List;
class A {
public static void main(String a[])
{
List<Map> data = new ArrayList<>();
}
}
|
Or
class A {
public static void main(String a[])
{
String s = " Hi there" ;
}
}
|
It looks fine, right? Yeah, because this is how things have been since the inception of Java. But there is one issue: It’s pretty obvious that if the type of the object is clearly mentioned at the right side of the expression, mentioning the same thing before the name of the variable makes it redundant. Plus, in the second example, you can see that it’s obvious that after the ‘=’ sign, it’s clearly a string as nothing except for a string can be enclosed in double inverted commas. Therefore, there arose a need to eliminate this redundancy and make variable declaration shorter, and more convenient.
How to declare local variables using LVTI:
Instead of mentioning the variable datatype on the left-side, before the variable, LVTI allows you to simply put the keyword ‘var’. For example,
import java.util.ArrayList;
import java.util.List;
class A {
public static void main(String ap[])
{
List<Map> data = new ArrayList<>();
}
}
|
Can be re-written as:
import java.util.ArrayList;
import java.util.List;
class A {
public static void main(String ap[])
{
var data = new ArrayList<>();
}
}
|
Use Cases
Here are the cases where you can declare variables using LVTI:
- In a static/instance initialization block
class A {
static
{
var x = "Hi there" ;
System.out.println(x)'
}
public static void main(String[] ax)
{
}
}
|
Output:
Oh hi there
- As a local variable
class A {
public static void main(String a[])
{
var x = "Hi there" ;
System.out.println(x)
}
}
|
Output:
Hi there
- As iteration variable in enhanced for-loop
class A {
public static void main(String a[])
{
int [] arr = new int [ 3 ];
arr = { 1 , 2 , 3 };
for (var x : arr)
System.out.println(x + "\n" );
}
}
|
Output:
1
2
3
- As looping index in for-loop
class A {
public static void main(String a[])
{
int [] arr = new int [ 3 ];
arr = { 1 , 2 , 3 };
for (var x = 0 ; x < 3 ; x++)
System.out.println(arr[x] + "\n" );
}
}
|
Output:
1
2
3
- As a return value from another method
class A {
int ret()
{
return 1 ;
}
public static void main(String a[])
{
var x = new A().ret();
System.out.println(x);
}
|
Output:
1
- As a return value in a method
class A {
int ret()
{
var x = 1 ;
return x;
}
public static void main(String a[])
{
System.out.println( new A().ret());
}
}
|
Output:
1
Error cases:
There are cases where declaration of local variables using the keyword ‘var’ produces an error. They’re mentioned below:
- Not permitted in class fields
- Not permitted for uninitialized local variables
class A {
public static void main(String a[])
{
var x;
}
}
|
- Not allowed as parameter for any methods
class A {
void show(var a)
{
}
}
|
- Not permitted in method return type
class A {
public var show()
{
return 1 ;
}
}
|
- Not permitted with variable initialized with ‘NULL’
variables initialized with 'Null'
can 't be declared using ' var'*/
class A {
public static void main(String a[])
{
var x = NULL;
}
|
Note: All these pieces of code run only on Java 10.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...