---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 22 Edition"
java_version: 22
chapter: "Chapter 3. Lexical Structure"
section: "3.9"
heading: "3.9. Keywords"
parent: "Chapter 3. Lexical Structure"
source: https://docs.oracle.com/javase/specs/jls/se22/html/jls-3.html#jls-3.9
id: "3.9"
url: https://jvm.obfuscated.software/jls/se22/3.9.html
raw: https://jvm.obfuscated.software/jls/se22/3.9.md
---

# 3.9. Keywords

51 character sequences, formed from ASCII characters, are reserved for use as keywords and cannot be used as identifiers ([§3.8](3.8.md)). 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](6.1.md)).

`true` and `false` are not keywords, but rather boolean literals ([§3.10.3](3.10.3.md)).

`null` is not a keyword, but rather the null literal ([§3.10.8](3.10.8.md)).

During the reduction of input characters to input elements ([§3.5](3.5.md)), 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:

1. The sequence is recognized as a terminal specified in a suitable context of the syntactic grammar ([§2.3](2.3.md)), as follows:

   - For `module` and `open`, when recognized as a terminal in a *ModuleDeclaration* ([§7.7](7.7.md)).

   - For `exports`, `opens`, `provides`, `requires`, `to`, `uses`, and `with`, when recognized as a terminal in a *ModuleDirective*.

   - For `transitive`, when recognized as a terminal in a *RequiresModifier*.

     For example, recognizing the sequence `requires` `transitive` `;` does not make use of *RequiresModifier*, so the term `transitive` is reduced here to an identifier and not a contextual keyword.

   - For `var`, when recognized as a terminal in a *LocalVariableType* ([§14.4](14.4.md)) or a *LambdaParameterType* ([§15.27.1](15.27.1.md)).

     In other contexts, attempting to use `var` as an identifier will cause an error, because `var` is not a *TypeIdentifier* ([§3.8](3.8.md)).

   - For `yield`, when recognized as a terminal in a *YieldStatement* ([§14.21](14.21.md)).

     In other contexts, attempting to use the `yield` as an identifier will cause an error, because `yield` is neither a *TypeIdentifier* nor a *UnqualifiedMethodIdentifier*.

   - For `record`, when recognized as a terminal in a *RecordDeclaration* ([§8.10](8.10.md)).

   - For `non-sealed`, `permits`, and `sealed`, when recognized as a terminal in a *NormalClassDeclaration* ([§8.1](8.1.md)) or a *NormalInterfaceDeclaration* ([§9.1](9.1.md)).

   - For `when`, when recognized as a terminal in a *Guard* ([§14.11.1](14.11.1.md)).

2. 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](3.2.md)). 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.
