Java是國際化的編程語言,印象中看到可以用任何語言做標識符党窜,于是驗證一下,然后動手試了一下
class 輸出類{
public static void main(String[] args) {
String 字符串變量1="你好,JAVA-1\n";
System.out.printf(字符串變量1);
輸出方法();
}
private static void 輸出方法(){
String 字符串變量2="你好荞胡,JAVA-2";
System.out.printf(字符串變量2);
}
}
編譯執(zhí)行結果
為什么可以java支持中文作為標識符呢?
書上都是這樣子寫的:
- 標識符由字母了嚎、數字泪漂、下劃線“_”廊营、美元符號“$”組成,第一個字符不能是數字
- 不能把java關鍵字作為標識符
- 標識符沒有長度限制
- 標識符對大小寫敏感
但是這樣子明顯和測試的結果不一致萝勤,標識符是中文也可以露筒,不一定是字母,那么我就去翻一下官網java教程說明http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.8
3.8. Identifiers
An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.
Identifier:
IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral
IdentifierChars:
JavaLetter {JavaLetterOrDigit}
JavaLetter:
any Unicode character that is a "Java letter"(//使用的Unicode字符集)
JavaLetterOrDigit:
any Unicode character that is a "Java letter-or-digit"
"Java letter" is a character for which the method Character.isJavaIdentifierStart(int) returns true.
"Java letter-or-digit" is a character for which the method Character.isJavaIdentifierPart(int)
returns true.
The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z(\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.The "Java digits" include the ASCII digits 0-9 (\u0030-\u0039).
Letters and digits may be drawn from the entire Unicode character set, which supports most writing scripts in use in the world today, including the large sets for Chinese(//中文), Japanese, and Korean. This allows programmers to use identifiers in their programs that are written in their native languages.(//重點在這里)
An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7), or a compile-time error occurs.
以上簡略翻譯一下官網文檔說明敌卓。內容都是簡單的慎式,只翻譯了重點。 不知道以前的java規(guī)定是字母假哎,還是翻譯教學有誤瞬捕。我覺得翻譯應該是加引號的‘‘Java 字母’’,而不是字母《婺ǎ現在官網的教程說明已經很清楚了肪虎,但是我們的教材還沒更新,本身學習就具有滯后性惧蛹,再加上教材的滯后性扇救,這樣顯得更加滯后了。
那有人也會說香嗓,用中文作標識符不好迅腔,如知乎的這個問題,那再教你可以用中文作標識符有什么意義呢靠娱?
中文本身不太適合做編程沧烈,標識符使用英文才能更好地標識。嘗試用中文去作標識符的意義在于深刻認識標識符是什么像云,Unicode是什么锌雀,編碼是什么,而不是書說是什么就是什么迅诬。