SO 上的许多地方都指向 Identifiers 上的 JLS 部分,但我对那里写的内容有疑问。
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 $ character 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.
我不明白这怎么可能都是真的。第一部分似乎准确地规定了允许使用哪些字符,而第二部分似乎说允许的字符要灵活得多。
我同意使用“包括”而不是“包括但不限于”表明它并不完全矛盾。但它也首先特指“Java 字母”/“Java 数字”,然后将其放宽为仅“字母”/“数字”。我的主要观点是缺乏清晰度,我想确认我的假设是什么意思。
最佳答案
根据问题Legal identifiers in Java可以看到有很多合法的标识符。
[对于使用罗马字母的语言]按照约定命名标识符时,仅使用字母数字字符,偶尔使用下划线。但是,可以使用大量字符。
第一段指的是 Java 程序员使用合理一致且可读的命名方案的代码风格或约定。您引用的第二段解释了 JVM 会接受大量其他字符 - 尽管您的程序员同事可能不赞成。
关于java 语言规范中的 java 有效标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32417922/