---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 21 Edition"
java_version: 21
chapter: "Chapter 3. Lexical Structure"
section: "3.5"
heading: "3.5. Input Elements and Tokens"
parent: "Chapter 3. Lexical Structure"
source: https://docs.oracle.com/javase/specs/jls/se21/html/jls-3.html#jls-3.5
id: "3.5"
url: https://jvm.obfuscated.software/jls/se21/3.5.html
raw: https://jvm.obfuscated.software/jls/se21/3.5.md
---

# 3.5. Input Elements and Tokens

The input characters and line terminators that result from Unicode escape processing ([§3.3](3.3.md)) and then input line recognition ([§3.4](3.4.md)) are reduced to a sequence of *input elements*.

```
Input:
  {InputElement} [Sub]

InputElement:
  WhiteSpace
  Comment
  Token

Token:
  Identifier
  Keyword
  Literal
  Separator
  Operator

Sub:
  the ASCII SUB character, also known as "control-Z"
```

Those input elements that are not white space or comments are *tokens*. The tokens are the terminal symbols of the syntactic grammar ([§2.3](2.3.md)).

White space ([§3.6](3.6.md)) and comments ([§3.7](3.7.md)) can serve to separate tokens that, if adjacent, might be tokenized in another manner.

For example, the input characters `-` and `=` can form the operator token `-=` ([§3.12](3.12.md)) only if there is no intervening white space or comment. As another example, the ten input characters `staticvoid` form a single identifier token while the eleven input characters `static void` (with an ASCII SP character between `c` and `v`) form a pair of keyword tokens, `static` and `void`, separated by white space.

As a special concession for compatibility with certain operating systems, the ASCII SUB character (`\u001a`, or control-Z) is ignored if it is the last character in the escaped input stream.

The *Input* production is ambiguous, meaning that for some sequences of input characters, there is more than one way to reduce the input characters to input elements (that is, to tokenize the input characters). Ambiguities are resolved as follows:

- A sequence of input characters that could be reduced to either an identifier token or a literal token is always reduced to a literal token.

- A sequence of input characters that could be reduced to either an identifier token or a reserved keyword token ([§3.9](3.9.md)) is always reduced to a reserved keyword token.

- A sequence of input characters that could be reduced to either a contextual keyword token or to other (non-keyword) tokens is reduced according to context, as specified in [§3.9](3.9.md).

- If the input character `>` appears in a type context ([§4.11](4.11.md)), that is, as part of a *Type* or an *UnannType* in the syntactic grammar ([§4.1](4.1.md), [§8.3](8.3.md)), it is always reduced to the numerical comparison operator `>`, even when it could be combined with an adjacent `>` character to form a different operator.

  Without this rule for `>` characters, two consecutive `>` brackets in a type such as `List<List<String>>` would be tokenized as the signed right shift operator `>>`, while three consecutive `>` brackets in a type such as `List<List<List<String>>>` would be tokenized as the unsigned right shift operator `>>>`. Worse, the tokenization of four or more consecutive `>` brackets in a type such as `List<List<List<List<String>>>>` would be ambiguous, as various combinations of `>`, `>>`, and `>>>` tokens could represent the `>>>>` characters.

Consider two tokens `x` and `y` in the resulting input stream. If `x` precedes `y`, then we say that `x` is *to the left of* `y` and that `y` is *to the right of* `x`.

For example, in this simple piece of code:

```
class Empty {
}
```

we say that the `}` token is to the right of the `{` token, even though it appears, in this two-dimensional representation, downward and to the left of the `{` token. This convention about the use of the words left and right allows us to speak, for example, of the right-hand operand of a binary operator or of the left-hand side of an assignment.
