51 character sequences, formed from ASCII characters, are reserved for use as keywords and cannot be used as identifiers (§3.8). Another 17 character sequences, also formed from ASCII characters, may be interpreted as keywords or as other tokens, depending on the context in which they appear.
Keyword:
ReservedKeyword
ContextualKeyword
ReservedKeyword:
(one of)
abstract continue for new switch assert default if package synchronized boolean do goto private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const float native super while _ (underscore)
ContextualKeyword:
(one of)
exports opens requires uses yield module permits sealed var non-sealed provides to when open record transitive with
The keywords const and goto are reserved, even though they are not currently used. This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs.
The keyword strictfp is obsolete and should not be used in new code.
The keyword _ (underscore) may be used in certain declarations in place of an identifier (§6.1).
true and false are not keywords, but rather boolean literals (§3.10.3).
null is not a keyword, but rather the null literal (§3.10.8).
During the reduction of input characters to input elements (§3.5), a sequence of input characters that notionally matches a contextual keyword is reduced to a contextual keyword if and only if both of the following conditions hold:
-
The sequence is recognized as a terminal specified in a suitable context of the syntactic grammar (§2.3), as follows:
-
For
moduleandopen, when recognized as a terminal in a ModuleDeclaration (§7.7). -
For
exports,opens,provides,requires,to,uses, andwith, when recognized as a terminal in a ModuleDirective. -
For
transitive, when recognized as a terminal in a RequiresModifier.For example, recognizing the sequence
requirestransitive;does not make use of RequiresModifier, so the termtransitiveis reduced here to an identifier and not a contextual keyword. -
For
var, when recognized as a terminal in a LocalVariableType (§14.4) or a LambdaParameterType (§15.27.1).In other contexts, attempting to use
varas an identifier will cause an error, becausevaris not a TypeIdentifier (§3.8). -
For
yield, when recognized as a terminal in a YieldStatement (§14.21).In other contexts, attempting to use the
yieldas an identifier will cause an error, becauseyieldis neither a TypeIdentifier nor a UnqualifiedMethodIdentifier. -
For
record, when recognized as a terminal in a RecordDeclaration (§8.10). -
For
non-sealed,permits, andsealed, when recognized as a terminal in a NormalClassDeclaration (§8.1) or a NormalInterfaceDeclaration (§9.1). -
For
when, when recognized as a terminal in a Guard (§14.11.1).
-
-
The sequence is not immediately preceded or immediately followed by an input character that matches JavaLetterOrDigit.
In general, accidentally omitting white space in source code will cause a sequence of input characters to be tokenized as an identifier, due to the "longest possible translation" rule (§3.2). For example, the sequence of twelve input characters p u b l i c s t a t i c is always tokenized as the identifier publicstatic, rather than as the reserved keywords public and static. If two tokens are intended, they must be separated by white space or a comment.
The rule above works in tandem with the "longest possible translation" rule to produce an intuitive result in contexts where contextual keywords may appear. For example, the sequence of eleven input characters v a r f i l e n a m e is usually tokenized as the identifier varfilename, but in a local variable declaration, the first three input characters are tentatively recognized as the contextual keyword var by the first condition of the rule above. However, it would be confusing to overlook the lack of white space in the sequence by recognizing the next eight input characters as the identifier filename. (This would mean that the sequence undergoes different tokenization in different contexts: an identifier in most contexts, but a contextual keyword and an identifier in local variable declarations.) Accordingly, the second condition prevents recognition of the contextual keyword var on the grounds that the immediately following input character f is a JavaLetterOrDigit. The sequence v a r f i l e n a m e is therefore tokenized as the identifier varfilename in a local variable declaration.
As another example of the careful recognition of contextual keywords, consider the sequence of 15 input characters n o n - s e a l e d c l a s s. This sequence is usually translated to three tokens - the identifier non, the operator -, and the identifier sealedclass - but in a normal class declaration, where the first condition holds, the first ten input characters are tentatively recognized as the contextual keyword non-sealed. To avoid translating the sequence to two keyword tokens (non-sealed and class) rather than three non-keyword tokens, and to avoid rewarding the programmer for omitting white space before class, the second condition prevents recognition of the contextual keyword. The sequence n o n - s e a l e d c l a s s is therefore tokenized as three tokens in a class declaration.
In the rule above, the first condition depends on details of the syntactic grammar, but a compiler for the Java programming language can implement the rule without fully parsing the input program. For example, a heuristic could be used to track the contextual state of the tokenizer, as long as the heuristic guarantees that valid uses of contextual keywords are tokenized as keywords, and valid uses of identifiers are tokenized as identifiers. Alternatively, a compiler could always tokenize a contextual keyword as an identifier, leaving it to a later phase to recognize special uses of these identifiers.