The var keyword in Java is used to declare a local variable with inferred type. Introduced in Java 10, it allows the compiler to determine the type of the variable based on the context in which it is used.
Table of Contents
- Introduction
varKeyword Syntax- Understanding
var - Examples
- Basic Usage
- Using
varwith Collections - Limitations of
var
- Real-World Use Case
- Conclusion
Introduction
The var keyword simplifies variable declarations by allowing the compiler to infer the type based on the assigned value. This leads to more concise and readable code without sacrificing type safety.
var Keyword Syntax
The syntax for declaring a variable using var is as follows:
var variableName = value;
Example:
var name = "John";
Understanding var
The var keyword can only be used for local variables inside methods, constructors, and lambda expressions. It cannot be used for instance variables, class variables, method parameters, or return types. The type of the variable is inferred from the assigned value.
Key Points:
- The type is inferred at compile time.
- The variable must be initialized at the point of declaration.
- It enhances code readability and reduces boilerplate code.
Examples
Basic Usage
To demonstrate the basic usage of the var keyword, we will declare a variable with an inferred type.
Example
public class VarExample {
public static void main(String[] args) {
var greeting = "Hello, World!";
System.out.println(greeting);
}
}
Output:
Hello, World!
Using var with Collections
The var keyword can be particularly useful when working with collections and complex generic types.
Example
import java.util.ArrayList;
import java.util.List;
public class VarWithCollections {
public static void main(String[] args) {
var list = new ArrayList<String>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
for (var item : list) {
System.out.println(item);
}
}
}
Output:
Apple
Banana
Cherry
Limitations of var
The var keyword cannot be used in certain scenarios, such as when the variable is not initialized at the point of declaration.
Example
public class VarLimitations {
public static void main(String[] args) {
var uninitializedVar; // Compilation error
uninitializedVar = 10;
System.out.println(uninitializedVar);
}
}
Output:
Compilation failed.
Real-World Use Case
Simplifying Code in Loops and Streams
In real-world applications, the var keyword can simplify code when working with loops, streams, and complex types.
Example
import java.util.List;
import java.util.stream.Collectors;
public class VarInStreams {
public static void main(String[] args) {
var numbers = List.of(1, 2, 3, 4, 5);
var evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println("Even numbers: " + evenNumbers);
}
}
Output:
Even numbers: [2, 4]
Conclusion
The var keyword in Java is a powerful feature that enhances code readability and reduces boilerplate code by allowing the compiler to infer the type of local variables. While it brings many benefits, it’s important to use var judiciously to maintain code clarity and avoid ambiguity. By understanding and using the var keyword, you can write more concise and readable Java code in your applications.