Java Programming/Syntax/Unicode Source
Most Java program text consists of ASCII characters, but any Unicode character can be used as part of identifier names, in comments, and in character and string literals. Unicode escape sequences may also be used to express a Unicode character.
For example, π (which is the Greek Lowercase Letter pi) is a valid Java identifier.
![]() |
double π = Math.PI;
|
and in a string literal
![]() |
String pi = "π";
|
π may also be represented in Java as the Unicode escape sequence \u03C0
. Thus, the following is a valid, but not very readable, declaration and assignment:
![]() |
double \u03C0 = Math.PI;
|
The following demonstrates the use of Unicode escape sequences in other Java syntax:
![]() |
// Declare Strings pi and quote which contain \u03C0 and \u0027 respectively:
String pi = "\u03C0"; String quote = "\u0027"; |
Note that a Unicode escape sequence functions just like any other character in the source code. E.g., \u0022
(double quote, ") needs to be quoted in a string just like ".
![]() |
// Declare Strings doubleQuote1 and doubleQuote2 which both contain " (double quote):
String doubleQuote1 = "\""; String doubleQuote2 = "\\u0022"; // "\u0022" doesn't work since """ doesn't work. |
See Unicode escape sequences for full details.