Java 类名中的有效字符

发布于 2024-07-05 08:07:40 字数 64 浏览 14 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

沉溺在你眼里的海 2024-07-12 08:07:40

您几乎可以使用任何字符,包括大多数 Unicode 字符! 确切的定义位于Java 语言规范第 3.8 节:标识符

标识符Java字母Java数字的无限长度序列,其中第一个必须是Java信。 ...

字母和数字可以从整个 Unicode 字符集中提取,...这允许程序员在用其母语编写的程序中使用标识符。

标识符不能与关键字 (§3.9)、布尔文字 (§3.10.3) 或空文字 (§3.10.7) 具有相同的拼写(Unicode 字符序列),否则会出现编译时错误.

但是,请参阅此问题了解是否应该这样做。

You can have almost any character, including most Unicode characters! The exact definition is in the Java Language Specification under section 3.8: Identifiers.

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.

However, see this question for whether or not you should do that.

霓裳挽歌倾城醉 2024-07-12 08:07:40

对于允许使用的名称类型,每种编程语言都有自己的一套规则和约定,Java 编程语言也不例外。 变量命名的规则和约定可总结如下:

  • 变量名称区分大小写。 变量的名称可以是任何合法标识符 - 无限长度的 Unicode 字母和数字序列,以字母、美元符号“$”或下划线字符“_”开头。 然而,约定是变量名始终以字母开头,而不是“$”或“_”。 此外,按照惯例,美元符号字符根本不会被使用。 您可能会发现某些情况下自动生成的名称将包含美元符号,但您的变量名称应始终避免使用它。 下划线字符也存在类似的约定; 虽然从技术上讲,变量名称以“_”开头是合法的,但不鼓励这种做法。 不允许使用空格。

  • 后续字符可以是字母、数字、美元符号或下划线字符。 惯例(和常识)也适用于该规则。 为变量选择名称时,请使用完整的单词而不是晦涩的缩写。 这样做将使您的代码更易于阅读和理解。 在许多情况下,它还会使您的代码自我记录; 例如,名为 cadence、speed 和 gear 的字段比缩写版本(例如 s、c 和 g)直观得多。 另请记住,您选择的名称不得是关键字或保留字。

  • 如果您选择的名称仅包含一个单词,请以全部小写字母拼写该单词。 如果它由多个单词组成,则将每个后续单词的第一个字母大写。 gearRatio 和 currentGear 名称是该约定的主要示例。 如果您的变量存储常量值,例如static final int NUM_GEARS = 6,则约定会略有变化,将每个字母大写并使用下划线字符分隔后续单词。 按照惯例,下划线字符不会在其他地方使用。

来自官方 Java 教程

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.

From the official Java Tutorial.

另类 2024-07-12 08:07:40

除了前面的答案之外,值得注意的是:

  1. Java 允许在符号名称中使用任何 Unicode 货币符号,因此以下内容都将起作用:

$var1
英镑var2
€var3

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

  1. 许多 C# 和 C++ 程序员采用将“I”放在接口前面的约定(又名C++ 中的纯虚拟类)。 在 Java 中这不是必需的,因此也不会这样做,因为 Implements 关键字可以非常清楚地表明某个东西是接口。

比较:

类 Employee : public IPayable //C++

with

员工类:IPayable //C#

类 Employee 实现 Payable //Java

  1. 许多项目都使用在字段名称前面放置下划线的约定,以便可以轻松地将它们与局部变量和参数区分开来,例如

私人双_salary;

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

私人双薪_;

Further to previous answers its worth noting that:

  1. Java allows any Unicode currency symbol in symbol names, so the following will all work:

$var1
£var2
€var3

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'

  1. Many C# and C++ programmers adopt the convention of placing 'I' in front of interfaces (aka pure virtual classes in C++). This is not required, and hence not done, in Java because the implements keyword makes it very clear when something is an interface.

Compare:

class Employee : public IPayable //C++

with

class Employee : IPayable //C#

and

class Employee implements Payable //Java

  1. Many projects use the convention of placing an underscore in front of field names, so that they can readily be distinguished from local variables and parameters e.g.

private double _salary;

A tiny minority place the underscore after the field name e.g.

private double salary_;

飘然心甜 2024-07-12 08:07:40

正如 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.

望笑 2024-07-12 08:07:40

我想在 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.

ぺ禁宫浮华殁 2024-07-12 08:07:40

标识符用于类名、方法名和变量名。 标识符可以是大写和小写字母、数字或下划线和美元符号字符的任何描述性序列。 它们不能以数字开头,以免与数字文字混淆。 同样,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

如若梦似彩虹 2024-07-12 08:07:40

类名应该是大驼峰式命名的名词,每个单词的第一个字母大写。 使用整个单词 — 避免使用首字母缩写词和缩写词(除非缩写词比长形式使用更广泛,例如 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

望笑 2024-07-12 08:07:40

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

  • Java 类名通常以大写字母开头。
  • Java 类名不能以数字开头。
  • 如果类名中有多个单词,例如“MyClassName”,每个单词都应以大写字母开头。 例如-“MyClassName”。此命名约定基于CamelCase 类型。

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

  • Java class names usually begin with a capital letter.
  • Java class names cannot begin with a number.
  • if there are multiple words in the class name like "MyClassName" each word should begin with a capital letter. eg- "MyClassName".This naming convention is based on CamelCase Type.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文