JVM Knowledge

3.9. Keywords

Raw .md Oracle

51 character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers (§3.8).

Keyword:
  (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)

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 _ (underscore) is reserved for possible future use in parameter declarations.

A variety of character sequences are sometimes assumed, incorrectly, to be keywords:

  • 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).
  • var, yield, and record are not keywords, but rather restricted identifiers (§3.8). var has special meaning as the type of a local variable declaration (§14.4) and the type of a lambda formal parameter (§15.27.1). yield has special meaning in a yield statement (§14.21); all invocations of a method named yield must be qualified so as to be distinguished from a yield statement. record has special meaning in a record declaration (§8.10).

A further ten character sequences are restricted keywords: open, module, requires, transitive, exports, opens, to, uses, provides, and with. These character sequences are tokenized as keywords solely where they appear as terminals in the ModuleDeclaration, ModuleDirective, and RequiresModifier productions (§7.7). They are tokenized as identifiers everywhere else, for compatibility with programs written before the introduction of restricted keywords. There is one exception: immediately to the right of the character sequence requires in the ModuleDirective production, the character sequence transitive is tokenized as a keyword unless it is followed by a separator, in which case it is tokenized as an identifier.