Java 类名中的有效字符
Java 类名中哪些字符有效? 还有哪些其他规则管理 Java 类名称(例如,Java 类名称不能以数字开头)?
What characters are valid in a Java class name? What other rules govern Java class names (for instance, Java class names cannot begin with a number)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您几乎可以使用任何字符,包括大多数 Unicode 字符! 确切的定义位于Java 语言规范第 3.8 节:标识符。
但是,请参阅此问题了解是否应该这样做。
You can have almost any character, including most Unicode characters! The exact definition is in the Java Language Specification under section 3.8: Identifiers.
However, see this question for whether or not you should do that.
来自官方 Java 教程。
From the official Java Tutorial.
除了前面的答案之外,值得注意的是:
我相信货币符号的使用起源于 C/C++,其中编译器添加到代码中的变量通常以“$”开头。 Java 中一个明显的例子是内部类的“.class”文件名,按照惯例,其格式为“Outer$Inner.class”。
比较:
with
和
极少数人在字段名称后面放置下划线,例如
Further to previous answers its worth noting that:
I believe the usage of currency symbols originates in C/C++, where variables added to your code by the compiler conventionally started with '$'. An obvious example in Java is the names of '.class' files for inner classes, which by convention have the format 'Outer$Inner.class'
Compare:
with
and
A tiny minority place the underscore after the field name e.g.
正如 Jason Cohen 所言,Java 语言规范在第 3.8 节中定义了合法标识符:
“标识符是 Java 字母和 Java 数字的无限长度序列,
其中第一个必须是 Java 字母。 [...]“Java 字母”是方法 Character.isJavaIdentifierStart(int) 返回 true 的字符。 “Java 字母或数字”是方法 Character.isJavaIdentifierPart(int) 返回 true 的字符。”
这有望回答您的第二个问题。关于您的第一个问题;我接受过老师和(到目前为止)的指导。据我记得)Java 编译器认为 Java 类名应该是以大写字母 AZ 开头的标识符,但我找不到任何可靠的来源。在使用 OpenJDK 尝试时,类名开头时没有任何警告。但是,当使用 $ 符号时,如果从 bash shell 进行编译,则必须转义它。
As already stated by Jason Cohen, the Java Language Specification defines what a legal identifier is in section 3.8:
"An identifier is an unlimited-length sequence of Java letters and Java digits, the
first of which must be a Java letter. [...] 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."
This hopefully answers your second question. Regarding your first question; I've been taught both by teachers and (as far as I can remember) Java compilers that a Java class name should be an identifier that begins with a capital letter A-Z, but I can't find any reliable source on this. When trying it out with OpenJDK there are no warnings when beginning class names with lower-case letters or even a $-sign. When using a $-sign, you do have to escape it if you compile from a bash shell, however.
我想在 bosnic 的答案中补充一点,任何有效的货币字符对于 Java 中的标识符都是合法的。 th€is 是合法标识符,€this 和 € 也是如此。 然而,我不知道如何编辑他或她的答案,所以我被迫发布这个琐碎的补充。
I'd like to add to bosnic's answer that any valid currency character is legal for an identifier in Java. th€is is a legal identifier, as is €this, and € as well. However, I can't figure out how to edit his or her answer, so I am forced to post this trivial addition.
标识符用于类名、方法名和变量名。 标识符可以是大写和小写字母、数字或下划线和美元符号字符的任何描述性序列。 它们不能以数字开头,以免与数字文字混淆。 同样,Java 区分大小写,因此 VALUE 是与 Value 不同的标识符。
有效标识符的一些示例包括:
AvgTemp 、count a4 、$test 、this_is_ok
无效变量名称包括:
2count、high-temp、Not/ok
Identifiers are used for class names, method names, and variable names. An identifiermay be any descriptive sequence of uppercase and lowercase letters, numbers, or theunderscore and dollar-sign characters. They must not begin with a number, lest they beconfused with a numeric literal. Again, Java is case-sensitive, so VALUE is a differentidentifier than Value.
Some examples of valid identifiers are:
AvgTemp ,count a4 ,$test ,this_is_ok
Invalid variable names include:
2count, high-temp, Not/ok
类名应该是大驼峰式命名的名词,每个单词的第一个字母大写。 使用整个单词 — 避免使用首字母缩写词和缩写词(除非缩写词比长形式使用更广泛,例如 URL 或 HTML)。
命名约定可以在这里阅读:
http://www.oracle.com/ technetwork/java/codeconventions-135099.html
Class names should be nouns in UpperCamelCase, with the first letter of every word capitalised. Use whole words — avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
The naming conventions can be read over here:
http://www.oracle.com/technetwork/java/codeconventions-135099.html