In the last issue of Heinz Kabutz's newsletter, #255 Java 10: Inferred Local Variables, it is shown that var
is not a reserved word in Java 10, because you can also use var
as an identifier:
public class Java10 {
var var = 42; // <-- this works
}
However, you cannot use i.e. assert
as an identifier, as in var assert = 2
, because assert
is a reserved word.
As it's told in the linked newsletter, the fact that var
is not a reserved word is good news, because this allows code from previous versions of Java that uses var
as an identifier to compile without problems in Java 10.
So, what's var
then? It's neither an explicit type nor a reserved word of the language, so it's allowed to be an identifier, however it does have a special meaning when used to declare a local variable in Java 10. What exactly do we call it in the context of a local variable declaration?
Additionally, apart from supporting backwards compatibility (by allowing older code that contains var
as an identifier to compile), are there other advantages to var
not being a reserved word?
var var = 42
orInteger var = 42
is exactly the same; it's just for the compiler here, so it's a type name. Notice that this is not very different thanInteger Integer = 42
(upvote to re-open, really wanted to make this one an answer too) – Eugene