Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
Normally you write ArrayList<String> list = new ArrayList<String>() — you write the type TWICE. With var, Java looks at the right side and figures out the type automatically. You just write var list = new ArrayList<String>(). Less typing, same result.
Real World Analogy
Ordering food at a restaurant. Normally you say "I want one plate of biryani." With var it's like pointing at the menu and saying "I want THAT." The waiter (Java compiler) knows exactly what you mean.
What is var?
var is a local variable type inference keyword introduced in Java 10. The compiler automatically infers the type from the right-hand side expression.
java
QUBITS OF DPK
var in Loops
java
QUBITS OF DPK
How var Works Under the Hood
var is resolved entirely at compile time. It is NOT dynamic typing like Python/JavaScript.
java
QUBITS OF DPK
Where var CANNOT Be Used
java
QUBITS OF DPK
Where var CAN Be Used
java
QUBITS OF DPK
var is NOT a Keyword (Technically!)
java
QUBITS OF DPK
Production Use Cases
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
30-Second Interview Answer
"var is local variable type inference introduced in Java 10. The compiler infers the type from the right-hand side at compile time — it is NOT dynamic typing, the bytecode is identical to explicit types with zero runtime overhead. var can only be used for local variables, loop variables, and try-with-resources — NOT for fields, parameters, or return types. Key trap: var infers the most specific type, so var list = new ArrayList<>() gives ArrayList not List, which can hurt flexibility."
Interview Questions & MAANG-Level Answers
Q1. What is var in Java and when was it introduced?
var is local variable type inference introduced in Java 10 (JEP 286). The compiler infers the variable's type from the right-hand side expression at compile time. var names = new ArrayList<String>() — compiler infers names is ArrayList<String>. The bytecode is identical to writing the explicit type — zero runtime overhead, same performance.
Q2. Is var dynamic typing like Python? Explain.
No, absolutely not. var is compile-time type inference — the type is fixed at compilation and cannot change. var x = 10 makes x an int forever — x = "hello" is a compile error. Python's dynamic typing allows a variable to hold any type at runtime. Java remains statically typed — var just lets the compiler write the type for you when it can be unambiguously inferred. Think of it as "the compiler fills in the type" not "the type is flexible".
Q3. Where can and cannot var be used?
Can: local variables with initialization, for loop variables (for (var i = 0; i < 10; i++)), for-each variables (for (var item : list)), try-with-resources (try (var conn = getConn())). Cannot: class fields, method parameters, method return types, variables without initialization (var x;), null initialization (var x = null), lambda parameters (sort of — Java 11 allows (var x, var y) -> x + y in lambdas), array shorthand (var arr = {1,2,3} is invalid).
Q4. What is the difference between var list = new ArrayList<String>() and List<String> list = new ArrayList<>()?
With var list = new ArrayList<String>(), the inferred type is ArrayList<String> — the most specific concrete type. With List<String> list = new ArrayList<>(), the declared type is List<String> — the interface. The second form is better practice: if you later change to LinkedList, only the right side changes. With var, if you accidentally call ArrayList-specific methods, switching implementation later breaks code. Program to interfaces, not implementations — prefer explicit interface types over var for collection declarations.
Q5. Is var a keyword in Java?
Technically NO — var is a reserved type name, not a keyword. This means you can still use var as a variable or method name (though you shouldn't): int var = 10 compiles without error. You cannot use it as a class name in new code. This design was intentional for backward compatibility — existing code that used var as a variable name would still compile after Java 10.