Tsallaka zuwa abun ciki
Justice Tech School

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

Is a var variable typed?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.