What would be your reaction if I tell you, you can use the Var keyword (not technically) (answer at the bottom ) to declare a variable in JAVA, just like JavaScript. How would you react?
Happy? Sad? I don't know whether you will be happy or sad, but I am sure about the fact that you would be confused. Does this mean we can leverage the features of dynamic typing which was available in languages like JavaScript and Python in JAVA? Mind-Boggling 🤯
Let's see whether this is really the case or are we missing something here.
The var keyword was introduced in Java 10. Type inference is used in var keyword in which it detects automatically the datatype of a variable based on the surrounding context. Let's understand this with an example given below:
class Test {
public static void main(String[] args)
{
// int
var x = 100;
// double
var y = 1.90;
// char
var z = 'p';
// string
var p = "Neha";
// boolean
var q = true;
// type inference is used in var keyword in which it
// automatically detects the datatype of a variable
System.out.println(x); //100
System.out.println(y); //1.9
System.out.println(z); //p
System.out.println(p); //Neha
System.out.println(q); //true
}
}
Now, take a look at this code.
It will put light on the question that Is the Var of Java same as the Var of JavaScript?
class test2 {
public static void main(String[] args) {
var a = 2 ;
System.out.println(a) ; //infers to be integer
a = "Hello" ;
System.out.println(a) ;
}
}
This code will give a compilation error at line 5. Precisely,
test2.java:5: error: incompatible types: String cannot be converted to int a = "Hello" ;
So it can be concluded from here that the Var of Java is not the same as the Var of JavaScript. Once the type of variable is inferred by the compiler it cannot change. The JavaScript equivalent code of this wouldn't have given any error.
So, Java is still a statically typed language, and the addition of var doesn't change this. With var, the Java compiler infers the type of the variable at compile-time, using type information obtained from the variable's initializer. The inferred type is then used as the static type of the variable. Typically, this is the same as the type you would have written explicitly, so a variable declared with var behaves exactly as if you had written the type explicitly.
So another question arises from here, What was the point of bringing in this feature?
The answer to this is, the use of var makes your code concise by reducing duplication, e.g. the name of the Class that comes in both the right and left-hand sides of assignments as shown in the following example:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Here, ByteArrayOutputStream repeats twice. We can eliminate that by using the var feature as shown below:
var bos = new ByteArrayOutputStream();
There are certain points we need to keep in mind while using var:
The local variable type inference (or Java 10 var keyword) can only be used to declare local variables, e.g. inside methods, on initializer code block, indexes in the enhanced for loop, lambda expressions, and local variables declared in a traditional for loop. We cannot use it for declaring formal variables and return types of methods, declaring member variables or fields, constructor formal variables, or any other kind of variable declaration.
One important thing to know is that even though var looks like a keyword, it's not really a keyword. Instead, it is a reserved type name. This means that code that uses var as a variable, method, or package name will not be affected.
Final Note from Author -
Hey Guys, this is my first Article here in HashNode, absolutely loving the platform ❤️. I am open to constructive criticism and do let me know if my article was of any use to you. I will surely try to bring out more in near future. The data here has been compiled from various sources like [openjdk.java.net] and [dzone.com].