Java类名称中的有效字符

时间:2020-03-05 18:54:12  来源:igfitidea点击:

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教程。

回答

我想补充一下bosnic的答案,即任何有效的货币字符对于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_;