My 2 cents to correct the question and answers:
-
The
var
is NOT a Java keyword. It’s a reserved type name. It seems not a big difference but in fact, it IS:var var = 0;
Here
var
is a variable name too, so thevar
can be used as a type name, but there is no restriction like for regular keyword (i.e. we can have a variable namedvar
too). -
The actual type of the variable IS decided by the Java compiler on the line where the variable is declared, at compile-time, but the type is NOT actually the exact implementation you use on the right side of the expression. See this code:
var i = true ? Integer.valueOf(1) : "ABC";
The Java compiler needs to pick a type for variable
i
which will satisfy both branches. It could be a)Object
, b)Serializable
, c)Comparable
, or combination, or all three. We don’t care and don’t know.