Java 10: Local variable type inference with var

var lets the compiler infer a local variable type from its initializer.

Code
var list = new ArrayList<String>();
list.add("hello");
var count = list.size();
var text = "count = " + count;
System.out.println(text + ", first = " + list.get(0));
Output
count = 1, first = hello
Advertisement

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