Java Tutorial: Unicode in Java

Xah Lee, 2005-02

One thing great about Java is that it is unicode based. That means, you can use characters from writing systems that are not latin-alphabet based (For example, Chinese), not just in data strings, but in function and variable names as well.

Here's a example code using unicode characters in class names and variable names.

class  {
    String  = "north";
    double π = 3.14159;
}

class UnicodeTest {
    public static void main(String[] arg) {
        方 x1 = new ();
        System.out.println( x1.北 );
        System.out.println( x1.π );
    }
}

Any char in a piece of java code can also be input as their unicode number, by starting with “\u” followed by its 4 digits hexadecimal code.

In the following example, “\u007b” is the left curly braces “{”, “\u0069” is lowercase “i”, “\u611b” is the Chinese char “愛” (meaning love).


class TestUniEsp \u007b
    static \u0069nt \u611b = 3;
    public static void main(String[] arg) {
    System.out.println( \u611b );
    }
}

Reference: Java Lang Spec: lexical↗.


See also:


Page created: 2005-01.
© 2005 by Xah Lee.
Xah Signet