官方正儿八经的变量创建与命名法——Creating Variables and Naming Them——java官方文档(双语对照)

54 阅读7分钟

Creating Variables and Naming Them

创建变量与命名

Variables

变量

As you learned in the previous section, an object stores its state in fields.

int cadence = 0;
int speed = 0;
int gear = 1;

The What Is an Object? discussion introduced you to fields, but you probably have still a few questions, such as: What are the rules and conventions for naming a field? Besides int, what other data types are there? Do fields have to be initialized when they are declared? Are fields assigned a default value if they are not explicitly initialized? We'll explore the answers to such questions in this section, but before we do, there are a few technical distinctions you must first become aware of. In the Java programming language, the terms ”field” and ”variable” are both used; this is a common source of confusion among new developers, since both often seem to refer to the same thing.

前面的章节中我们提到对象的状态会储存在它的字段里。

int cadence = 0;
int speed = 0;
int gear = 1;

对象是什么?中向你介绍了什么是对象的字段,但可能你还有些疑惑,诸如:字段命名的规范和约定?除了int还有什么数据类型能用?字段声明的时候必须要初始化吗?当字段没有被初始化赋值的时候会被分配默认值吗?本章我们就会探寻此类问题的答案,但我们首先需要知道一件事,java与其他语言有所不同——在java语言中,“字段”和“变量”两个词都会被用到,这通常会给开发新人造成困惑,因为这两词看似经常指的是同一个东西。

The Java programming language defines the following kinds of variables:

  • Instance Variables (Non-Static Fields)  Technically speaking, objects store their individual states in ”non-static fields”, that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.

java语言中定义了下列几种变量:

  • 实例变量 (非静态字段) 从技术角度讲,对象会把它自己的状态储存在“非静态字段” 中,即字段声明时不带有static关键字。非静态字段也被称为实例变量,这是因为它的值在每一个实例(或者说对象)中都不相同。比如一台自行车的currentSpeed 时速字段与另一台自行车的时速是互不影响的。

  • Class Variables (Static Fields)  A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.

  • 类变量(静态字段)static关键字修饰的字段都可以称作成员变量,这将会告知编译器不论这个对象被创建多少次,这个变量也只能出现一个副本。一个代表某种类自行车有几个档位的字段static int numGears = 6;会创建一个静态的字段。当确定自行车的挡数不会发生改变时,还可以额外添加一个final关键字

  • Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.

  • 局部变量 正如同对象会将他的状态储存在字段中一样,方法也会将它临时的状态储存为局部变量。声明局部变量的语法和声明字段的语法也一样(比如int count = 0;)。对于变量是否是局部变量没有特殊的关键字标注,只取决于它声明的位置-即介于方法的开和闭两个括号之间,如此声明的局部变量只对它声明时所在的方法内部可见,而对方法外部不可见。

  • Parameters You've already seen examples of parameters, both in the Bicycle class and in the main method of the ”Hello World!” application. Recall that the signature for the main method is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as ”variables” not ”fields”. This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you'll learn about later in the tutorial.

  • 参数 其实我们已经见过“参数”了,前文中提到自行车类和打印”Hello World!”的主方法都有参数出现。 回想一下主方法的方法签名是 public static void main(String[] args)。这里的args变量就是这个方法的参数。不得不提的就是参数应该分类成“变量”而不算“字段”,对于后面的教程中提到的所有接受参数的构造来说都是如此(包括构造器和异常处理器)。

Having said that, the remainder of this tutorial uses the following general guidelines when discussing fields and variables. If we are talking about ”fields in general” (excluding local variables and parameters), we may simply say ”fields”. If the discussion applies to ”all of the above”, we may simply say ”variables”. If the context calls for a distinction, we will use specific terms (static field, local variables, etc.) as appropriate. You may also occasionally see the term ”member” used as well. A type's fields, methods, and nested types are collectively called its members.

话说回来,剩下的教程中我们采用以下准则来讨论字段和变量:如果我们讲“一般的字段”(不包括局部变量和参数),我们会简称成“字段”。如果我们讨论的是“以上全部”,我们就简称“变量”。如果我们需要在文章的上下文中做区分,我们会酌情使用它专用的术语称呼(静态字段,局部变量等)。你偶尔也会看到“成员”这个词出现,类型的字段,方法,和嵌套类型都被统称为它的“成员”。

Naming Variables

变量的命名

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:

每一种编程语言都有自己的一套命名规则和约定来指定哪种名字是你可以起的,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.

  • 变量命名是大小写敏感的(区分大小写的)。一个变量的名字可以是任何合法字符——即一串以字母、符号或者下划线开头的长度不限的Unicode字母和数字序列。我们约定用字母做完变量的开头,而不得使用符号或者下划线开头的长度不限的Unicode 字母和数字序列。我们约定用字母做完变量的开头,而不得使用或 “_”。此外,通常我们约定是不要使用美元符号。你可能会发现有些自动生成的名字会带有美元符号美元符号。你可能会发现有些自动生成的名字会带有美元符号,单你自己应该避免用它给变量命名。对于下划线也是如此;从技术角度讲用下划线做变量名开头是合法的,但是不鼓励这样做。而空格不允许做变量开头。

  • 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.

  • 后面的字符可以是字母,数字,美元符号或者下划线。约定(通常情况下)也是如此。当给你的变量起名字时,使用完整的变量而不是缩写,以此来是你的代码简单易懂,很多情况下它也会让你的代码自解释;例如用节奏、速度、档位等词做字段名是比s,c,g这样的缩写更好理解的。同时还要注意你选的词不能是关键字或者保留字。

  • 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.

  • 如果你的命名只由一个单词组成,请用全小写来拼写这个单词。当由不止一个单词组成名字时,除首字母以外后续每一个单词的首字母大写。比如 gearRatio 和 currentGear 。如果你的变量储存一个常量,如static final int NUM_GEARS = 6,命名约定有点小变化,你需要将每个字母大写同时用下划线分割后续每个单词,这也是下划线唯一一个被约定使用的地方。