What is var (local variable type inference) in Java?

Var (local variable type inference) is a feature introduced in Java 10 that allows developers to declare local variables without explicitly specifying their type. Instead of declaring a variable with its type, you can use the keyword 'var', and the Java compiler will infer the type based on the assigned value. This leads to more concise and readable code while maintaining type safety.

For example:

var name = "John Doe"; // The compiler infers 'name' as a String var age = 30; // The compiler infers 'age' as an int var list = new ArrayList(); // The compiler infers 'list' as ArrayList

Java var local variable type inference Java 10 programming coding