What is `var` in Java, and what does it actually do to a local variable declaration?
answer
- Java 10, JEP 286, local-variable type inference
- Compiler infers type from the initializer
- Still static / strongly typed — no runtime cost
- Reserved type name, not a real keyword
- Mental model: compiler pastes the inferred type
basics
~20 svar lets you declare a local variable without writing its type; the compiler figures out the type from the value you assign. The variable still has a fixed type — var just saves you from typing it.
solid answer
~40 s`var` (introduced in Java 10) is local-variable type inference. Instead of writing the type on the left, you write `var` and the compiler infers the static type from the initializer expression. For example `var list = new ArrayList<String>();` makes `list` an `ArrayList<String>`. It is purely a compile-time convenience: the variable is still strongly, statically typed — there is no dynamic typing and no runtime cost. The inferred type is fixed at declaration, so you cannot later assign a value of an incompatible type. It only works for local variables that have an initializer; it cannot be used for fields, method parameters, or return types. It is best used when the type is obvious from the right-hand side, improving readability without losing type safety.
go deeper
Knows var lets the compiler infer a local variable's type from its initializer, and that the variable is still typed (you cannot change its type later).
Can explain it is compile-time only with no runtime cost, arrived in Java 10, and works only on initialized local variables.
Articulates that var is a reserved type name (not a keyword) for backward compatibility, that the inferred type is fixed and statically checked, and discusses readability trade-offs.
Frames var within the language's design philosophy (reducing ceremony without sacrificing type safety), can reason about how it interacts with denotable vs non-denotable types and team-wide style guidance.
## The problem `var` solves In Java every variable has a **type** — a label that tells the compiler what kind of value it holds (an `int`, a `String`, an `ArrayList<String>`, etc.). Historically you had to write that type **twice** in many declarations: ```java ArrayList<String> names = new ArrayList<String>(); ``` The `ArrayList<String>` on the left is redundant — the right side already says exactly what is being created. This verbosity gets worse with long generic types like `Map<String, List<Customer>>`. ## What `var` is `var` (added in **Java 10**, JEP 286) is **local-variable type inference**. You replace the explicit type with the keyword `var`, and the compiler **infers** (works out) the type for you from the **initializer** — the expression on the right of the `=`: ```java var names = new ArrayList<String>(); // compiler infers ArrayList<String> var count = 5; // inferred as int var message = "hello"; // inferred as String ``` ## Key point: it is still statically typed "**Static type**" means the type is known and fixed at **compile time** (before the program runs). `var` does **not** make Java dynamically typed like JavaScript or Python. After `var count = 5;`, the variable `count` is exactly an `int` — forever. This will not compile: ```java var count = 5; count = "text"; // ERROR: String cannot be assigned to int ``` There is **no runtime cost** and **no change to the bytecode**: the compiler simply substitutes the inferred type. `var` is purely syntactic sugar for the developer. ## `var` is a 'reserved type name', not a keyword Unusually, `var` is **not** a true keyword. It is a *reserved type name*. This means existing code that used `var` as a variable name, method name, or package name (`int var = 3;`) still compiles. You just cannot name a *class* or *interface* `var`. ## The mental model Think of `var` as the compiler doing copy-paste for you: it looks at the initializer, computes that expression's type, and pastes it where `var` is. If you could not write a valid declaration by pasting that type in, `var` cannot be used there. This explains every restriction (covered in the related questions): there must be an initializer (nothing to infer from otherwise), and it works only for locals (fields and parameters have no single initializer the compiler can read at the declaration).
- Does `var` work in older Java versions like Java 8?No. `var` for local variables arrived in Java 10. Earlier releases (8, 9) do not support it; you must write the explicit type.
- Is there any performance difference between `var x = ...` and the explicit-type version?None. They compile to identical bytecode — `var` is resolved entirely at compile time, so it is a pure source-level convenience.
saying these in an interview costs you the question
- Saying `var` makes Java dynamically typed or like JavaScript/Python
- Claiming `var` has a runtime performance cost or changes bytecode
- Thinking you can reassign a `var` to a different/incompatible type later
- Calling `var` a true keyword (it is a reserved type name)