Author
var Keyword & Type Inference (Java 10+)The var keyword was introduced in Java 10 to support local variable type inference. It helps make Java code cleaner, shorter, and easier to read, especially when working with complex data types.
var in Java?The var keyword allows the Java compiler to automatically determine the type of a variable based on the value assigned to it.
In simple words: You don’t need to explicitly write the data type — Java automatically figures it out at compile time.
Example
var number = 10; // int
var name = "Durgesh"; // String
var price = 99.99; // double
In this example:
number is treated as intname is treated as Stringprice is treated as doubleLocal variable type inference means the Java compiler automatically detects the variable type based on the assigned value.
Example:
var name = "Java";
var age = 25;
var price = 99.99;
? Internally, Java treats it as:
String name = "Java";
int age = 25;
double price = 99.99;

var?The var keyword is useful when working with complex data types, especially in modern Java applications. It helps reduce unnecessary code and makes programs easier to read and write. Instead of repeating long type declarations, developers can rely on the compiler to infer the type automatically. It is particularly helpful when dealing with generics, collections, and streams, where type declarations can become lengthy and harder to maintain.
Before Java 10:
In older versions of Java, developers had to explicitly mention the full type, even if it was long and repetitive.
Map<String, List<Integer>> map = new HashMap<>();
With var:
With Java 10, we can use var to let the compiler infer the type automatically.
var map = new HashMap<String, List<Integer>>();

Benefits:
Using var provides several advantages in modern Java development:
var Worksvar with the actual data typeExample:
var x = 10; // int
x = 20; // allowed
x = "Hello"; // error
varThere are some important rules you must follow when using the var keyword in Java.
var must be initialized at the time of declarationvar x = 10; // valid
var y; // error
? Compiler needs a value to infer type.
class Test {
var x = 10; // error (not allowed here)
}
null Alonevar cannot be used with null only
var x = null; // error
? Because the compiler cannot determine the type.
var x = 10;
x = "Hello"; // invalid
Declaration without assignment is not allowed
var x; // invalid
Cannot be used in method parameters or return types
public var method() { } // invalid
void show(var x) { } // invalid
var with CollectionsUsing var with collections makes the code shorter and easier to read, especially when working with generics.
Example with var
var list = new ArrayList<String>();
list.add("Java");
Without var
ArrayList<String> list = new ArrayList<>();
var in Loopsvar can be used in loops to simplify variable declaration and improve readability.
for (var i = 0; i < 5; i++) {
System.out.println(i);
}
? The compiler automatically treats i as an int, so you don’t need to declare the type.
var with Streamsvar is also useful when working with streams, where types can become complex.
var names = List.of("Ram", "Shyam", "Amit");
names.stream()
.forEach(n -> System.out.println(n));
? Using var makes the code cleaner and easier to read, especially with collections and streams.
varvarvar can make code confusingvar when the assigned value does not clearly indicate the typevarUsing var correctly can make your code cleaner, but misuse can reduce readability. Follow these best practices:
Use When Type is Obvious Use var when the type can be easily understood from the value
var count = 10;
Use for Complex Generic Types
Helpful when working with long and complex type declarations
var map = new HashMap<String, List<Integer>>();
Use in Loops
Makes loop variables simpler and cleaner
for (var item : list) {
System.out.println(item);
}
Avoid When Type is Unclear
Do not use var if the type is not obvious from the method
var data = getData(); // unclear type
Avoid Overuse
Using var everywhere can reduce readability
var a = 10;
var b = 20;
var c = a + b;
? Too much use of var can make code harder to understand.
var vs Dynamic Typing| Feature | var (Java) |
Dynamic Typing |
|---|---|---|
| Type checking | Done at compile time | Done at runtime |
| Type safety | Strong (errors caught early) | Weak (errors at runtime) |
| Type change | Not allowed | Allowed |
| Language type | Statically typed | Dynamically typed |

? Java is still statically typed, even with var.
for and enhanced for-loopsThe var keyword in Java helps make code shorter, cleaner, and more modern by enabling local variable type inference. It reduces unnecessary type declarations while keeping Java’s strong type system intact.
? When used properly, var improves code quality and developer productivity. However, overusing it can reduce readability, so it should be used wisely.