Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using UTF-8 characters in JAVA variable-names

I would like to know that can I use my native language characters (or String) as JAVA variable names ? So, I had tested as below with Myanmar Unicode.

    public static void main(final String[] args) {
    String ဆဆဆ = "မောင်မောင်";
    System.out.println("ကောင်းသောနေ.ပါ " + ဆဆဆ);
}

This code show my successful message as 'ကောင်းသောနေ.ပါ မောင်မောင်'. But in below code with another variable name (it also my native language String).....

    public static void main(final String[] args) {
    String တက်စတင်း = "မောင်မောင်";
    System.out.println("ကောင်းသောနေ.ပါ " + တက်စတင်း);
}

that produce compile time error in my Ecliplse IDE.

Multiple markers at this line - Syntax error on tokens, delete these tokens - Got an exception - Unexpected character 0x103a in identifier

Any suggestion ? Why would this problem has occured ? Thanks for reading my question patiently.......

like image 842
Cataclysm Avatar asked Feb 03 '14 11:02

Cataclysm


People also ask

Can we use special characters in variable in Java?

No spaces or special characters are allowed in the variable names of Java. Variable names may contain 0 to 9 numbers (if not at the beginning).

Can UTF-8 handle special characters?

Since ASCII bytes do not occur when encoding non-ASCII code points into UTF-8, UTF-8 is safe to use within most programming and document languages that interpret certain ASCII characters in a special way, such as / (slash) in filenames, \ (backslash) in escape sequences, and % in printf.

What is the use of UTF-8 in Java?

UTF-8 represents a variable-width character encoding that uses between one and four eight-bit bytes to represent all valid Unicode code points. A code point can represent single characters, but also have other meanings, such as for formatting.

Can UTF-8 support all characters?

UTF-8 supports any unicode character, which pragmatically means any natural language (Coptic, Sinhala, Phonecian, Cherokee etc), as well as many non-spoken languages (Music notation, mathematical symbols, APL). The stated objective of the Unicode consortium is to encompass all communications.


1 Answers

Java 6 merely supports Unicode 4.0, a quite old version of Unicode which doesn't include Myanmar (or at least only rudimentary). Extended Myanmar support was added to Unicode 5.1, which is supported in Java 7.

To resolve this issue, install JDK 7 if you haven't already, and configure your Eclipse project to compile as Java 7 (Project->Properties->Java Compiler). Please note that code compiled as Java 7 cannot be run on Java 6 or lower.

like image 129
Njol Avatar answered Oct 20 '22 07:10

Njol