Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode escape syntax in Java

Tags:

In Java, I learned that the following syntax can be used for mentioning Unicode characters that are not on the keyboard (eg. non-ASCII characters):

(\u)(u)*(HexDigit)(HexDigit)(HexDigit)(HexDigit)

My question is: What is the purpose of (u)* in the above syntax?

One use case that I understood which represents Yen symbol in Java is:

char ch = '\u00A5';
like image 981
user3265048 Avatar asked Feb 03 '14 08:02

user3265048


People also ask

What is unicode escape in Java?

Unicode Escapes. A compiler for the Java programming language ("Java compiler") first recognizes Unicode escapes in its input, translating the ASCII characters \u followed by four hexadecimal digits to the UTF-16 code unit (§3.1) of the indicated hexadecimal value, and passing all other characters unchanged.

How do you escape unicode?

A unicode escape sequence is a backslash followed by the letter 'u' followed by four hexadecimal digits (0-9a-fA-F). It matches a character in the target sequence with the value specified by the four digits. For example, ”\u0041“ matches the target sequence ”A“ when the ASCII character encoding is used.

How do you escape code in Java?

A character with a backslash (\) just before it is an escape sequence or escape character. We use escape characters to perform some specific task. The total number of escape sequences or escape characters in Java is 8.


2 Answers

Interesting question. Section 3.3 of the JLS says:

UnicodeEscape:
    \ UnicodeMarker HexDigit HexDigit HexDigit HexDigit

UnicodeMarker:
    u
    UnicodeMarker u

which translates to \\u+\p{XDigit}{4}

and

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.

So you're right, there can be one or more u after the backslash. The reason is given further down:

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.

So this input

 \u0020ä

becomes

 \uu0020\u00e4

The first uu means here "this was a unicode escape sequence to begin with" while the second u says "An automatic tool converted a non-ASCII character to a unicode escape."

This information is useful when you want to convert back from ASCII to unicode: You can restore as much of the original code as possible.

like image 129
Aaron Digulla Avatar answered Nov 04 '22 21:11

Aaron Digulla


It means you can add as many u as you want - for example these lines are equivalent:

char ch = '\u00A5';
char ch = '\uuuuu00A5';
char ch = '\uuuuuuuuuuuuuuuuuu00A5';

(and all compile)

like image 36
assylias Avatar answered Nov 04 '22 22:11

assylias