In Java, every variable has a type. You declare a variable by placing the type first, followed by the name of the variable. Here are some examples:

在Java中,每个变量都有一个类型。你可以用把类型写在前面,后面紧跟变量名的方式来声明变量。下面是一些例子:

double salary;

int vacationDays;

long earthPopulation;

boolean done;

Notice the semicolon at the end of each declaration. The semicolon is necessary because a declaration is a complete Java statement.

注意每一个声明后面的分号。由于每个声明都是一条完整的Java语句,所以分号是必须写的。

A variable name must begin with a letter and must be a sequence of letters or digits. Note that the terms "letter" and "digit" are much broader in Java than in most languages. A letter is defined as 'A'–'Z', 'a'–'z', '_', or any Unicode character that denotes a letter in a language. For example, German users can use umlauts such as 'ä' in variable names; Greek speakers could use a p. Similarly, digits are '0'–'9' and any Unicode characters that denote a digit in a language. Symbols like '+' or '©' cannot be used inside variable names, nor can spaces. All characters in the name of a variable are significant and case is also significant. The length of a variable name is essentially unlimited.

变量名必须以字母开头的一串字母或数字序列。注意,Java中所说的“字母”和“数字”比大多数编程语言要宽泛的多。字母是指'A'–'Z', 'a'–'z', '_',或者任何在某种语言中表示某个字母的Unicode字符。例如,德国人可以在变量名中使用元音变音'ä';希腊人可以使用p。类似的,数字也是'0'–'9'或者任何在某种语言中表示一个数位的Unicode字符。诸如'+' or '©'此类的字符也不能被用于变量名,空格也不可以。所有的变量名中的字符乃至大小写都是有意义的。变量名的长度几乎是没有限制的。

TIP

 

If you are really curious as to what Unicode characters are "letters" as far as Java is concerned, you can use the isJavaIdentifierStart and isJavaIdentifierPart methods in the Character class to check.

提示

 

如果你真的像关心Java那样对于何种Unicode字符属于“字母”感到好奇的话,不妨用Character类中的isJavaIdentifierStart和isJavaIdentifierPart方法去检查。

You also cannot use a Java reserved word for a variable name. (See Appendix A for a list of reserved words.)

你不能用Java保留字作为变量名。

You can have multiple declarations on a single line:

你可以在同一行声明多个变量:

int i, j; // both are integers

However, we don't recommend this style. If you declare each variable separately, your programs are easier to read.

但是,我们并不推荐使用这种方式。如果你分别声明每一个变量,你的程序可读性更好。

NOTE

 

As you saw, names are case sensitive, for example, hireday and hireDay are two separate names. In general, you should not have two names that only differ in their letter case. However, sometimes it is difficult to come up with a good name for a variable. Many programmers then give the variable the same name of the type, such as

Box box; // ok--Box is the type and box is the variable name

Other programmers prefer to use an "a" prefix for the variable:

Box aBox;

注释

 

如你所见,命名是区分大小写的,例如,hireday和hireDay是两个不同的名字。通常,你不应该仅仅通过大小写来区分两个命名。但是,有时候很难给一个变量起一个好名字。很多程序员就用与类型名相同的名字来给变量命名,例如:

Box box; // ok--Box 是个类型而 box 是变量名

有的程序员喜欢给变量名加上一个“a”前缀:

Box aBox;

Initializing Variables变量初始化

After you declare a variable, you must explicitly initialize it by means of an assignment statement—you can never use the values of uninitialized variables. For example, the Java compiler flags the following sequence of statements as an error:

声明变量以后,你必须明确的用一个赋值语句对其初始化——你绝不能够使用一个没有初始化的变量。例如,Java编译器将会标记如下语句为一个错误:

int vacationDays;

System.out.println(vacationDays); // ERROR--variable not initialized

You assign to a previously declared variable by using the variable name on the left, an equal sign (=), and then some Java expression that has an appropriate value on the right.

给变量赋值时,你可以把先前声明好的变量名写在左边,再写一个等号(=),然后右边紧跟一些Java具有适当值的表达式。

int vacationDays;

vacationDays = 12;

You can both declare and initialize a variable on the same line. For example:

你也可以把变量的声明和初始化写在一行,例如:

int vacationDays = 12;

Finally, in Java you can put declarations anywhere in your code. For example, the following is valid code in Java:

最后要说明的,在Java中,你可以把变量的声明放在你代码的任何地方。比如,下面的代码在Java中是正确的:

double salary = 65000.0;

System.out.println(salary);

int vacationDays = 12; // ok to declare a variable here

In Java, it is considered good style to declare variables as closely as possible to the point where they are first used.

在Java中,一种比较好的方式就是声明变量以后紧接着指出该变量第一次使用的位置。

C++ NOTE

 

C and C++ distinguish between the declaration and definition of variables. For example,

int i = 10;

is a definition, whereas

extern int i;

is a declaration. In Java, no declarations are separate from definitions.

C++ 附注

 

C和C++中区分声明和定义。例如:

int i = 10;

是一个定义,而

extern int i;

是一个声明。在Java中,没有独立于定义的声明。

Constants常量

In Java, you use the keyword final to denote a constant. For example,

在Java中,你可以用关键字final来指明一个常量,例如:

public class Constants

{

public static void main(String[] args)

{

final double CM_PER_INCH = 2.54;

double paperWidth = 8.5;

double paperHeight = 11;

System.out.println("Paper size in centimeters: "

+ paperWidth * CM_PER_INCH + " by " + paperHeight * CM_PER_INCH);

}

}

The keyword final indicates that you can assign to the variable once, and then its value is set once and for all. It is customary to name constants in all upper case.

关键字final指出你可以给变量赋值一次,而其值也只能设置一次。通常习惯上将常量名全部大写。

It is probably more common in Java to want a constant that is available to multiple methods inside a single class. These are usually called class constants. You set up a class constant with the keywords static final. Here is an example of using a class constant:

在Java中,我们或许更多时候需要一个能被一个类中的多个方法使用的常量。这种常量被称之为类常量。类常量用关键字static final来设置。下面是一个使用类常量的例子:

public class Constants2

{

public static void main(String[] args)

{

double paperWidth = 8.5;

double paperHeight = 11;

System.out.println("Paper size in centimeters: "

+ paperWidth * CM_PER_INCH + " by " + paperHeight * CM_PER_INCH);

}

public static final double CM_PER_INCH = 2.54;

}

Note that the definition of the class constant appears outside the main method. Thus, the constant can also be used in other methods of the same class. Furthermore, if (as in our example) the constant is declared public, methods of other classes can also use the constant—in our example, as Constants2.CM_PER_INCH.

注意到对类常量的定义出现在了主方法的外面。正因为如此,类常量也可以被同类的其他方法访问。此外,如果(如我们的例子)常量被声明为public,那么其他类的方法也可以使用这个常量——在我们的例子中,就像Constants2中的CM_PER_INCH一样。

C++ NOTE

 

const is a reserved Java keyword, but it is not currently used for anything. You must use final for a constant.

C++ 注释

 

const是一个Java预留的关键字,但是现在不再用于任何场合。你必须使用final来定义常量。


文章来源:http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!300.entry