Java: Unicode in Java

By Xah Lee. Date: . Last updated: .

Java allows function or variable names to be NON-ASCII characters, such as Greek letters or math symbols.

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.π );
    }
}

Unicode Character Escape Syntax

Any character in source code can also be represented by its Unicode number. By starting with \u followed by its 4 digits hexadecimal code.

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

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

To find Unicode character and it's code point and hexadecimal, see Unicode Characters ☯ ⚡ ∑ ♥ 😄

File Encoding

When saving file, make sure you save it as utf8 encoding. Your editor should have a option to do so.

If you have errors in compiling, try javac -encoding UTF8 myFileName

javac

〔►see Unicode Basics: What's Character Set, Character Encoding, UTF-8?