var in Java : Local Variable type inference
var was introduced in Java 10
Before jumping into the concept, lets glance through few terms which might help in understanding the var.
When we declare a variable or initialize a variable, it will be holding some type of data.
The data can be of integer type or double type or string type or when we want to use collections, say map or set or list, we initialize a variable of collections say map or set or list.
Every variable has some type which indicates the compiler which type of value the variable is storing and in the same way, we the programmers also understand the purpose of the variable.
Eg :
Now consider this example :
If there are similar type of variables to be used in the code, it can result in following
- make code look congested
- reduce code readability
- induce boilerplate declarations
So with onset of Java 5, Type inference was introduced
Type inference enables the compiler to determine (The word used commonly is inferred) the type of data that the variable will be using or storing, at compile time, based on declaration of a variable or initialization of a variable.
Determining the type of variable at compile time means type of variable will be determined when we are writing the code itself.
Declaration of variable is done using :
[ {variabletype} {variableName}]
As datatype of variable s is String, s can store values of String type.
As listOfValues is of list type and datatype is String, listOfValues can store a list of String values.
With the onset of Java 7, diamond operator <> was introduced.
Any difference?
Yes, we no longer had to provide the type on right hand side. It helped us to avoid writing the type which was getting repeated. Compiler will detect the type present in the declaration on left hand side.
Now coming to local variable.
Variable present in method or block is termed as a local variable.
This particular variable will not be accessible outside the method where it is declared or initialized.
With this brief understanding, lets explore the feature called Local variable Type inference
Local variable Type inference can be utilized using var
var is a reserved type name, not a keyword.
As a result, any code where var is used as a variable name, method name or package name will not be affected.
var can be used when we are directly initializing a variable i.e. the variable should not be initialized to null or in other words a variable having non-null initializer.
When var is used as type for a variable, compiler looks at the right hand side of the initialization and uses the given type.
Effective and valid usage of var includes
- as local variable
- as an index in for loop and enhanced for loop
- try-with resources
Examples of using var
Can we reassign a var variable?
Yes, we definitely can, provided the new variable to which we are assigning is of the same type as that of var variable.
Although, there are few limitations in using var
- Not valid to be used in function definition
- Not valid method return type
- Should not be used with fields of a class.
Consider in a payment application, for eg:
There is a class User, which has a variable ‘totalAmount’ is used with var. User class is used in multiple other classes and ‘totalAmount’ is interpreted in a different way in different classes. It will be not be possible to achieve the desired result.
Few pain points which appears on using var.
Consider this example
var inp = displayValue();
- Are we able to determine the type of variable ‘inp’ ?
- Is it improving code readability ?
- Will the user be able to understand , without checking the method displayValue()
The Answer will be : NO
Shall we use var?
It’s usage depends more on the developer, provided the best practices are maintained, where developer or any one who later uses the code, can understand the use of any particular var variable , where no confusion arises as to why it has been used.
Particularly var should be used when it is initialized with a value whose type is very clear and direct. And that particular variable is not affecting the overall functioning and understanding of the codebase.
Thanks for reading !!
If you find it interesting and informative. Please click on claps 👏 on this post and share for greater reach.
Share your experiences ,ideas and feedback by commenting on this post.
Feel free to follow me at Subhodip Basu for more interesting articles.
Feel free to connect with me on LinkedIn