Java: Unicode in Java

By Xah Lee. Date: . Last updated: .

Class, function, variable names can be NON-ASCII characters, such as Greek letters or math symbols.

class X {
    public static void main(String[] arg) {

    String  = "north";
    double π = 3.14159;

    System.out.println( 北 );
    System.out.println( π );
    }
}

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 X2 \u007b
    static \u0069nt \u03b1 = 3;
    public static void main(String[] arg) {
    System.out.println( \u03b1 );
    }
}

In the above example,

To find Unicode character and its codepoint 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?