HuntExams Academy logo
JavaBeginner#fundamentals#java10

What is the difference between var and explicit typing in Java 10+?

Answer

var lets the compiler infer the local variable's type from the initializer at compile time; it's still statically typed, not dynamic. It can only be used for local variables with an initializer, not fields or method parameters.

Example

var list = new ArrayList<String>(); // inferred as ArrayList<String>
list.add("hi");
// var x; // error: no initializer to infer from

Detailed explanation

In Java 10+, var lets the compiler infer a local variable’s type from its initializer instead of requiring you to write the type explicitly. For example, var list = new ArrayList<String>(); declares list with the inferred type ArrayList<String>. With explicit typing, you write the declared type yourself, such as List<String> list = new ArrayList<String>();. Both forms are statically typed: the compiler checks assignments and method calls before execution. var is not dynamic typing, and the variable cannot later change to an unrelated type.

Under the hood, type inference happens during compilation; var does not introduce runtime type discovery or extra runtime overhead. Once the compiler determines the variable’s type, normal Java type-checking rules apply. For example, var count = 10; infers int, so count = "ten"; fails to compile. An important distinction is that var uses the initializer’s type rather than choosing an abstraction for you. A variable inferred as ArrayList<String> cannot later hold a LinkedList<String>, whereas an explicitly declared List<String> variable can. The chosen declared type also affects which methods are accessible and can affect overload resolution.

For ordinary local variable declarations, var requires an initializer and cannot infer a type from null alone. Therefore, var value; and var value = null; are invalid. It cannot replace field types, method return types, or ordinary method and constructor parameter types. It is also supported in suitable loop declarations, including enhanced for loops, and try-with-resources declarations. Java 11 additionally allows var for lambda parameters, which is a separate feature and does not permit var in ordinary method signatures.

Use var when the initializer makes the type clear and removing repetition improves readability. Prefer explicit typing when the intended abstraction matters or a method call hides the resulting type. Also remember that var items = new ArrayList<>(); infers ArrayList<Object>, not ArrayList<String>, because no declared target type supplies the element type. Interviewers typically look for understanding of compile-time inference, usage restrictions, and readability trade-offs rather than a claim that either style is always better.

Key points to remember

  • var performs compile-time type inference; Java remains statically typed.
  • Explicit typing states the declared type, while var derives it from the initializer.
  • Ordinary var local declarations require an initializer other than null alone.
  • var cannot declare fields, return types, or ordinary method parameters; Java 11 permits it for lambda parameters.
  • Use explicit interface types when you want assignments and accessible operations governed by that abstraction.

Common mistakes candidates make

  • Assuming a var variable can later hold values of unrelated types.
  • Writing var x;, var x = null;, or using var as a field type.
  • Assuming var list = new ArrayList<>(); infers String elements from later add calls.

Likely follow-up questions

Can a variable declared with var be reassigned?

Yes, unless it is also declared final, but every assignment must be compatible with its inferred type. For example, var count = 10; allows count = 20; but not count = "twenty";.

Is var list = new ArrayList<String>() equivalent to List<String> list = new ArrayList<>()?

Both initialise an ArrayList containing strings, but their declared variable types differ. The first infers ArrayList<String>; the second declares List<String>, allowing later assignment of another List<String> implementation.

Can var be used with a lambda expression?

A standalone declaration such as var f = x -> x + 1; is invalid because the lambda needs a target functional interface type. Java 11 permits var in lambda parameter lists, such as (var x) -> x + 1, when the surrounding context supplies that target type.

Interview tip: Explain compile-time inference first, show one valid declaration, and contrast an inferred ArrayList<String> with an explicitly declared List<String>.

Related Interview Questions

1
JavaIntermediate#inner-classes

What is the difference between a static nested class and a non-static inner class?

Open
2
JavaIntermediate#inner-classes

What are anonymous inner classes used for?

Open
3
JavaIntermediate#inner-classes

What is a local class in Java?

Open