var Type Inference
var lets the compiler infer a local variable type. It is still statically typed.
Example
public class Main {
public static void main(String[] args) {
var n = 5;
var s = "hi";
var list = new java.util.ArrayList<String>();
list.add(s);
System.out.println(n + " " + list);
}
}Result
5 [hi]
Exercise
Infer the type.
x = 10;Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.