using UTF-8 characters in JAVA variable-names

2019-06-18 04:41发布

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.......

5条回答
我只想做你的唯一
2楼-- · 2019-06-18 05:21

You can use your native language characters as JAVA identifiers if that characters are letters or digits (first character may by a letter only). To make sure if a character is a letter or digit, use Character.isLetter and Character.isDigit.

Be careful to use encodings which support your characters. Best choice is UTF-8.

In Strings, you can use any characters.

查看更多
Summer. ? 凉城
3楼-- · 2019-06-18 05:28

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.

查看更多
虎瘦雄心在
4楼-- · 2019-06-18 05:34

How about using

Charset.forName("UTF-8").encode(myString)

查看更多
欢心
5楼-- · 2019-06-18 05:40

I am not sure but try setting this OS environment
variable JAVA_TOOL_OPTIONS and see if it helps.

C:\Work>echo %JAVA_TOOL_OPTIONS%
-Dfile.encoding=UTF8
查看更多
聊天终结者
6楼-- · 2019-06-18 05:45

Configure charset in your IDE. If you are using Eclipse go to properties and search for charset or encoding. Configure it to be UTF-8 for your java editor.

查看更多
登录 后发表回答