Java Var Keyword — Free Java Tutorial
Learn Java Var Keyword in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn Java Var Keyword in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
Java Var Keyword in Java
Java 10 introduced var for local variable type inference. The compiler infers the type from the initializer — you write less boilerplate without losing static typing.
var is not a keyword in the traditional sense — it is a reserved type name. The type is determined at compile time and cannot change (still statically typed).
var can only be used for local variables with an initializer. It cannot be used for fields, method parameters, return types, or variables initialized to null.
Use var when the type is obvious from context (var list = new ArrayList<String>()) and avoid it when it hides important type information.
Java Var Keyword — Syntax
// Instead of: ArrayList<String> list = new ArrayList<>();
var list = new ArrayList<String>(); // type inferred
// Instead of: Map.Entry<String, Integer> entry = ...
for (var entry : map.entrySet()) { ... }
// Works for any local variable
var count = 0; // int
var price = 29.99;
Learn Java Var Keyword step by step with Syllab's free interactive Java tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Java course →