Java 10 comes with the new Local Variable Type Inference. The token var
can be used to reduce the boilerplate required when declaring a variable. e.g.
var s = "hello";
According to What type of token is exactly "var" in Java 10? this new token is not a "keyword" but rather is a "reserved type name". As such the word "var" can still be used as a variable name which maintains backwards compatibility with existing code.
var var = "you can do this";
When the "module" feature was introduced in Java 9 the type of this new token (along with its 9 other related tokens) were called a "restricted keywords". Which is to say they were only considered keywords under certain context specific restrictions. e.g. you can still have variables called module.
When new language features were added to C++ in such a way that they did not clobber existing user defined symbols they were called "context-sensitive keywords".
Is there a conceptual difference between the new "reserved type name" var
token in Java 10 and a "context-sensitive keyword" or "restricted keyword". Which is to say isn't the new var
token really just a keyword under certain context specific restrictions. If that is the case why wasn't it simply added to the list of "restricted keywords"?
To add to my confusion further the current draft version of the JLS says that:
The character sequence var is generally treated as an identifier, but in certain special cases acts as if it were a keyword instead.
That definition certainly sounds like a "restricted keyword".
A restricted keyword has a specific meaning in the language, but only if you use that word in a specific way. For example, if you write\r\n\r\n<code>requires other.stuff;</code>\r\n\r\nyou tell Java that your program won't run unless it has access to some other code (the code contained in <code>other.stuff</code>).
var is a reserved type name var is not a keyword, It's a reserved type name. We can create a variable named “var”.
In earlier versions of Java, the underscore ("_") has used as an identifier or to create a variable name. Since Java 9, the underscore character is a reserved keyword and can't be used as an identifier or variable name.
print has to be defined as method (otherwise your program would not compile), so for java it would be an ID.
The very next sentence of the section you quote (3.8: Keywords) is:
A further ten character sequences are restricted keywords: open, module, requires, transitive, exports, opens, to, uses, provides, and with.
Note that var
is not on this list. The mention of var
in this section was included precisely to make it clear that while in some cases it may act like a restricted keyword, and while it may sound that way to you in your informal reading of the specification, it is not.
Context-sensitive keywords is one of the tools we have at our disposal for evolving the language in a compatible way; reserved identifiers are another. In this particular case, either could have been applied, and in the end, the latter tool was considered (for the purposes of specification and compiler implementation) to be preferable.
The specification, like most compiler implementations, separates lexical, syntactic, and typing concerns. Keywords are handled primarily at the level of lexer and parser productions; reserved type names are checked later in the compilation process, during type analysis, and can share the parser productions with non-reserved names.
From the perspective of a developer who is neither a spec author nor a compiler implementor, the difference is largely theoretical; the desired effect can be accomplished with either path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With