java 其中哪些是有效的变量名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27338623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:38:40  来源:igfitidea点击:

Which of these are valid variable names?

javanetbeans

提问by Vilches Felipe

This is a question from a Java test I took at University

这是我在大学参加的 Java 测试中的一个问题

I. publicProtected

一、公共保护

II. $_

二、$_

III. _identi#ficador

三、_identi#ficador

I've. Protected

我有。受保护

I'd say I, II, and I've are correct. What is the correct answer for this?

我会说 I、II 和 Ive 是正确的。对此的正确答案是什么?

Source of the question in spanish: Teniendo la siguiente lista de identificadores de variables, ?Cuál (es) es (son) válido (s)?

西班牙语问题的来源:Teniendo la siguiente lista de identificadores de variables, ?Cuál (es) es (son) válido (s)?

回答by en_Knight

From the java documentation:

从java文档:

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.

变量名区分大小写。变量的名称可以是任何合法标识符 — Unicode 字母和数字的无限长度序列,以字母、美元符号“$”或下划线字符“ 开头。然而,惯例是始终以字母开头您的变量名,而不是“$”或“”。此外,按照惯例,从不使用美元符号字符。您可能会发现某些情况下自动生成的名称将包含美元符号,但您的变量名称应始终避免使用它。下划线字符也有类似的约定;虽然以“_”开头变量名在技术上是合法的,但不鼓励这种做法。不允许使用空格。后续字符可能是字母、数字、美元符号或下划线字符。约定(和常识)也适用于这条规则。为变量选择名称时,请使用完整的单词而不是含糊的缩写。这样做将使您的代码更易于阅读和理解。在许多情况下,它还会使您的代码自记录;例如,名为 cadence、speed 和 gear 的字段比缩写版本(如 s、c 和 g)直观得多。另请记住,您选择的名称不能是关键字或保留字。

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

In short: yes, you're right. You can use underscores, dollarsigns, and characters to start a variable name. After the first letter of the variable name, you can also use numbers. Note that using dollar signs is generally not good practice.

简而言之:是的,你是对的。您可以使用下划线、美元符号和字符作为变量名的开头。在变量名的第一个字母之后,也可以使用数字。请注意,使用美元符号通常不是一个好习惯。

From your comment, you said that your teacher rejected "II". Under your question, II is perfectly fine (try it, it will run). However, if the question on your test asked which are "good" variable names, or which variable names follow common practice, then II would be eliminated as explained in the quotation above. One reason for this is that dollar signs do not make readable variable names; they're included because internally Java makes variables that use the dollar sign.

从你的评论中,你说你的老师拒绝了“II”。根据您的问题,II 完全没问题(尝试一下,它会运行)。但是,如果您的测试中的问题询问哪些是“好”变量名称,或者哪些变量名称遵循惯例,那么 II 将被排除,如上面引用中所述。原因之一是美元符号不会使变量名可读;它们被包含在内是因为 Java 在内部生成使用美元符号的变量。

What is the meaning of $ in a variable name?

变量名中的$是什么意思?

As pointed out in the comments, IV is not a good name either, since the lower case version "protected" is a reserved keyword. With syntax highlighting, you probably wouldn't get the two confused, but using keyword-variations as variable names is certainly one way to confuse future readers

正如评论中指出的那样,IV 也不是一个好名字,因为小写版本“protected”是一个保留关键字。使用语法高亮,您可能不会将两者混淆,但使用关键字变体作为变量名肯定是混淆未来读者的一种方式

回答by Mirza Arzeesh Zaman

Private protected public are reserved or keywords in java.. Use _ or to use that those words.. example int public_x; int protected_x; String private_s;

私有保护 public 是 Java 中的保留或关键字。使用 _ 或使用那些词。例如 int public_x; int protected_x; 字符串 private_s;