About “var” Keyword In Java
Starting in Java 10, We have the option of using the keyword “var” instead of the type for local variables under certain conditions.
As we know very well, If we need to declare an integer variable to hold the age of a customer then we will do as follows in Java:
int age= 32;
Here the type of age is Integer and int is the keyword.
Similarly, If we need to declare a String variable to hold the name of a customer then we will declare as follows:
String customerName = “Hemanth”;
Here the type of customerName is String.
The above two statements we can rewrite as follows:
var age = 32;var customerName = “Hemanth”;
The formal name of this feature is local variable type inference.
When we type var, we are instructing the compiler to determine the type for us. The compiler looks at the code on the line of the declaration and uses it to infer the type. In other languages like JavaScript, var to mean a variable that can take on any type at runtime. In Java, var is still a specific type defined at compile time. It does not change type at runtime.
As stated earlier, this var keyword we can use under certain conditions. Let’s see what are those conditions
A var is used as a local variable in a constructor, method and initialization block.
public class Customer{
//Inside a Constructor
public Customer(){
var customerName = "Hemanth";
var age = 32;
}
//Inside a Method
public void customerDetails(){
var customerName = "Hemanth";
var age = 32;
}
// Inside instance initatilization
{
var customerName = "Hemanth";
var age = 32;
}
}
A var cannot be used in constructor parameters, method parameters , instance variables, or class variables.
public void customerDetails(var a){ // Does not compile}
A var is always initialized on the same line where it is declared.
public void customerDetails(){
var a; // Does not compile
a = 10;
}
The value of a var can change but the type can’t
public void customerDetails(){
var a = 10;
a = 5;
a = “hemanth”; //Does not compile
}
A var cannot be initialized with a null value without a type
public void customerDetails(){
var customerName=”hemanth”;
customerName = null;
var age = 32;
age = null; // Does not compile
}
A var is not permitted in a multiple variable declarations.
public void customerDetails(){
var age = 32, customerName = “hemanth”; // Does not compile
}
A var is a reserved type name but not a reserved word, meaning it can be used as an identifier except as a class , interface, or enum name.
public class Var{ // class name is Var
public int var(){ // method name is var
var var=3;
return var;
}
public void Var(){ // method name is Var
Var var= new Var(); // reference variable name is var
}
}
A var can be used in Lambda Expression as follows
Predicate<String> p = (var x) -> true;
Take Away:
Starting Java 10, Java has introduced the local variable type inference using “var” keyword.
"Learning Science shows that learning a bit each day exponentially
improves your ability to retain your knowledge". --- Keep Learning.
Comments
Post a Comment