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

# 3.3. Unicode Escapes

A compiler for the Java programming language ("Java compiler") first recognizes Unicode escapes in its raw input, translating the ASCII characters `\u` followed by four hexadecimal digits to a *raw input character* which denotes the UTF-16 code unit ([§3.1](3.1.md)) for the indicated hexadecimal value. One Unicode escape can represent characters in the range U+0000 to U+FFFF; representing supplementary characters in the range U+010000 to U+10FFFF requires two consecutive Unicode escapes. All other characters in the compiler's raw input are recognized as raw input characters and passed unchanged.

This translation step results in a sequence of Unicode input characters, all of which are raw input characters (any Unicode escapes having been reduced to raw input characters).

```
UnicodeInputCharacter:
  UnicodeEscape
  RawInputCharacter

UnicodeEscape:
  \ UnicodeMarker HexDigit HexDigit HexDigit HexDigit

UnicodeMarker:
  u {u}

HexDigit:
  (one of)
  0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

RawInputCharacter:
  any Unicode character representable in UTF-16
```

The `\`, `u`, and hexadecimal digits here are all ASCII characters.

The *UnicodeInputCharacter* production is ambiguous because an ASCII `\` character in the compiler's raw input could be reduced to either a *RawInputCharacter* or the `\` of a *UnicodeEscape* (to be followed by an ASCII `u`). To avoid ambiguity, for each ASCII `\` character in the compiler's raw input, input processing must consider the most recent raw input characters that resulted from this translation step:

- If the most recent raw input character in the result was itself translated from a Unicode escape in the compiler's raw input, then the ASCII `\` character is eligible to begin a Unicode escape.

  For example, if the most recent raw input character in the result was a backslash that arose from a Unicode escape `\u005c` in the raw input, then an ASCII `\` character appearing next in the raw input is eligible to begin another Unicode escape.

- Otherwise, consider how many backslashes appeared contiguously as raw input characters in the result, back to a non-backslash character or the start of the result. (It is immaterial whether any such backslash arose from an ASCII `\` character in the compiler's raw input or from a Unicode escape `\u005c` in the compiler's raw input.) If this number is even, then the ASCII `\` character is eligible to begin a Unicode escape; if the number is odd, then the ASCII `\` character is not eligible to begin a Unicode escape.

  For example, the raw input `"\\u2122=\u2122"` results in the eleven characters `" \ \ u 2 1 2 2 = ™ "` because while the second ASCII `\` character in the raw input is not eligible to begin a Unicode escape, the third ASCII `\` character is eligible, and `\u2122` is the Unicode encoding of the character `™`.

If an eligible `\` is not followed by `u`, then it is treated as a *RawInputCharacter* and remains part of the escaped Unicode stream.

If an eligible `\` is followed by `u`, or more than one `u`, and the last `u` is not followed by four hexadecimal digits, then a compile-time error occurs.

The character produced by a Unicode escape does not participate in further Unicode escapes.

For example, the raw input `\u005cu005a` results in the six characters `\ u 0 0 5 a`, because `005c` is the Unicode value for a backslash. It does not result in the character `Z`, which is Unicode value `005a`, because the backslash that resulted from processing the Unicode escape `\u005c` is not interpreted as the start of a further Unicode escape.

Note that `\u005cu005a` cannot be written in a string literal to denote the six characters `\ u 0 0 5 a`. This is because the first two characters resulting from translation, `\` and `u`, are interpreted in a string literal as an illegal escape sequence ([§3.10.7](3.10.7.md)).

Fortunately, the rule about contiguous backslash characters helps programmers to craft raw inputs that denote Unicode escapes in a string literal. Denoting the six characters `\ u 0 0 5 a` in a string literal simply requires another `\` to be placed adjacent to the existing `\`, such as `"\\u005a is Z"`. This works because the second `\` in the raw input `\\u005a` is not eligible to begin a Unicode escape, so the first `\` and the second `\` are preserved as raw input characters, as are the next five characters `u 0 0 5 a`. The two `\` characters are subsequently interpreted in a string literal as the escape sequence for a backslash, resulting in a string with the desired six characters `\ u 0 0 5 a`. Without the rule, the raw input `\\u005a` would be processed as a raw input character `\` followed by a Unicode escape `\u005a` which becomes a raw input character `Z`; this would be unhelpful because `\Z` is an illegal escape sequence in a string literal. (Note that the rule translates `\u005c\u005c` to `\\` because the translation of the first Unicode escape to a raw input character `\` does not prevent the translation of the second Unicode escape to another raw input character `\`.)

The rule also allows programmers to craft raw inputs that denote escape sequences in a string literal. For example, the raw input `\\\u006e` results in the three characters `\ \ n` because the first `\` and the second `\` are preserved as raw input characters, while the third `\` is eligible to begin a Unicode escape and thus `\u006e` is translated to a raw input character `n`. The three characters `\ \ n` are subsequently interpreted in a string literal as `\ n` which denotes the escape sequence for a linefeed. (Note that `\\\u006e` may be written as `\u005c\u005c\u006e` because each Unicode escape `\u005c` is translated to a raw input character `\` and so the remaining raw input `\u006e` is preceded by an even number of backslashes and processed as the Unicode escape for `n`.)

The Java programming language specifies a standard way of transforming a program written in Unicode into ASCII that changes a program into a form that can be processed by ASCII-based tools. The transformation involves converting any Unicode escapes in the source text of the program to ASCII by adding an extra `u` - for example, `\uxxxx` becomes `\uuxxxx` - while simultaneously converting non-ASCII characters in the source text to Unicode escapes containing a single `u` each.

This transformed version is equally acceptable to a Java compiler and represents the exact same program. The exact Unicode source can later be restored from this ASCII form by converting each escape sequence where multiple `u`'s are present to a sequence of Unicode characters with one fewer `u`, while simultaneously converting each escape sequence with a single `u` to the corresponding single Unicode character.

A Java compiler should use the `\uxxxx` notation as an output format to display Unicode characters when a suitable font is not available.
