?

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