我在讨论变量命名的论坛中进行了一次有趣的讨论。
抛开惯例,我注意到变量具有 Unicode 字符的名称是合法的,例如以下是合法的:
int \u1234;
但是,例如,如果我给它命名#,它会产生错误。根据Sun's tutorial如果“以字母、美元符号“$”或下划线字符“_”开头,则有效。
但是 unicode 1234 是一些埃塞俄比亚字符。那么真正定义为“字母”的是什么?
最佳答案
Unicode 标准定义了什么算作字母。
来自Java Language Specification, section 3.8 :
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.
A "Java letter" is a character for which the method Character.isJavaIdentifierStart(int) returns true. A "Java letter-or-digit" is a character for which the method Character.isJavaIdentifierPart(int) returns true.
来自isJavaIdentifierPart
的Character
文档:
Determines if the character (Unicode code point) may be part of a Java identifier as other than the first character. A character may be part of a Java identifier if any of the following are true:
- it is a letter
- it is a currency symbol (such as '$')
- it is a connecting punctuation character (such as '_')
- it is a digit
- it is a numeric letter (such as a Roman numeral character)
- it is a combining mark
- it is a non-spacing mark
- isIdentifierIgnorable(codePoint) returns true for the character
关于Java Unicode 变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1422655/