Java类名中的有效字符

Java类名中的有效字符

Valid characters in a Java class name

Java类名中有哪些字符有效? 还有哪些其他规则管理Java类名称(例如,Java类名称不能以数字开头)?


你几乎可以拥有任何角色,包括大多数Unicode角色!确切的定义在Java语言规范的3.8节:标识符中。

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. ...

Letters and digits may be drawn from the entire Unicode character set, ... 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.

但是,请看这个问题是否应该这样做。


Every programming language has its own set of rules and conventions for the kinds of names that you're allowed to use, and the Java programming language is no different. The rules and conventions for naming your variables can be summarized as follows:

  • Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign"$", or the underscore character"_". The convention, however, is to always begin your variable names with a letter, not"$" or"_". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with"_", this practice is discouraged. White space is not permitted.

  • Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence, speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word.

  • If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.

来自官方Java教程。


继前面的答案之后,值得注意的是:

  • Java允许在符号名称中使用任何Unicode货币符号,因此以下内容都可以使用:
  • $var1
    £var2
    €var3

    我相信货币符号的使用源于C / C ++,其中编译器添加到代码中的变量通常以'$'开头。 Java中一个明显的例子是内部类的'.class'文件的名称,按照惯例,它们的格式为'Outer $ Inner.class'

  • 许多C#和C ++程序员采用在接口前面放置'I'的惯例(在C ++中也称为纯虚拟类)。这在Java中不是必需的,因此也没有完成,因为implements关键字在某些东西是接口时非常清楚。
  • 相比:

    class Employee : public IPayable //C++

    class Employee : IPayable //C#

    class Employee implements Payable //Java

  • 许多项目使用在字段名称前放置下划线的约定,以便可以容易地将它们与局部变量和参数区分开来,例如,
  • private double _salary;

    少数人在字段名称之后放置下划线,例如

    private double salary_;


    正如Jason Cohen所说,Java语言规范定义了3.8节中的合法标识符:

    "标识符是Java字母和Java数字的无限长度序列,
    第一个必须是Java字母。 [...]'Java letter'是Character.isJavaIdentifierStart(int)方法返回true的字符。"Java letter-or-digit"是Character.isJavaIdentifierPart(int)方法返回true的字符。"

    这有希望回答你的第二个问题。关于你的第一个问题;我已经被老师和(据我记忆所知)Java编译器教过,Java类名称应该是以大写字母A-Z开头的标识符,但我找不到任何可靠的来源。使用OpenJDK进行尝试时,在使用小写字母或甚至$ -sign开始类名时没有警告。使用$ -sign时,如果从bash shell编译,则必须将其转义。


    我想补充一下bosnic的答案,即任何有效的货币字符对于Java中的标识符都是合法的。 th€是一个合法的标识符,也就是这个,以及€。但是,我无法弄清楚如何编辑他或她的答案,所以我被迫发布这个微不足道的补充。


    What other rules govern Java class names (for instance, Java class names cannot begin with a number)?

    • Java类名通常以大写字母开头。
    • Java类名称不能以数字开头。
    • 如果类名中有多个单词,如"MyClassName",则每个单词都应以大写字母开头。 eg-"MyClassName"。这个命名约定基于CamelCase Type。


    标识符用于类名,方法名和变量名。标识符可以是大写和小写字母,数字或者下划线和美元符号字符的任何描述性序列。它们不能以数字开头,以免它们与数字文字混淆。同样,Java区分大小写,因此VALUE是一个区分标识符而不是Value。
    有效标识符的一些示例是:

    AvgTemp,计数a4,$ test,this_is_ok

    无效的变量名称包括:

    2count,高温,不是/ ok


    类名应该是UpperCamelCase中的名词,每个单词的首字母大写。使用整个单词 - 避免使用缩写词和缩写词(除非缩写词比长词形式使用得更广泛,例如URL或HTML)。
    命名约定可以在这里阅读:

    http://www.oracle.com/technetwork/java/codeconventions-135099.html


    推荐阅读