var lets the compiler infer a local variable type from its initializer.
var list = new ArrayList<String>();
list.add("hello");
var count = list.size();
var text = "count = " + count;
System.out.println(text + ", first = " + list.get(0));
count = 1, first = hello
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-26