Java is a strongly typed language. This means that every variable must have a declared type. There are eight primitive types in Java. Four of them are integer types; two are floating-point number types; one is the character type char, used for code units in the Unicode encoding scheme (see the section on the char type); and one is a boolean type for truth values.

Java是一種強(qiáng)類(lèi)型語(yǔ)言。這意味著每個(gè)變量必須聲明類(lèi)型。Java中有八種原始類(lèi)型。其中4種是整數(shù)類(lèi)型;兩種是浮點(diǎn)數(shù)類(lèi)型;一種是字符類(lèi)型char,適用于Unicode編碼方案中的代碼單元(參見(jiàn)本節(jié)中的char類(lèi)型小節(jié));還有一種是用來(lái)表示真值的布爾類(lèi)型。

NOTE注釋

?

Java has an arbitrary precision arithmetic package. However, "big numbers," as they are called, are Java objects and not a new Java type. You see how to use them later in this chapter.

Java有一個(gè)任意精度的算術(shù)包。顧名思義,“大數(shù)”是一個(gè)Java對(duì)象,而并非新的Java類(lèi)型。你將在本章后面看到如何使用它們。

Integers整數(shù)

The integer types are for numbers without fractional parts. Negative values are allowed. Java provides the four integer types shown in Table 3-1.

整數(shù)類(lèi)型是四種無(wú)小數(shù)部分的數(shù)字,允許負(fù)值。Java提供表3-1所示的四種整數(shù)類(lèi)型。

Table 3-1. Java Integer Types

類(lèi)型

存儲(chǔ)空間

范圍(閉區(qū)間)

Int

4 字節(jié)

–2,147,483,648 to 2,147,483, 647 (超過(guò)20億)

Short

2字節(jié)

–32,768 to 32,767

Long

8字節(jié)

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Byte

1字節(jié)

–128 to 127

In most situations, the int type is the most practical. If you want to represent the number of inhabitants of our planet, you'll need to resort to a long. The byte and short types are mainly intended for specialized applications, such as low-level file handling, or for large arrays when storage space is at a premium.

大多數(shù)情況下,int類(lèi)型是最實(shí)用的。如果你想表示我們星球上居民的數(shù)量,你就需要訴諸于long類(lèi)型。Byte和short類(lèi)型主要用于一些特殊的應(yīng)用,比如較低級(jí)的文件處理,或者當(dāng)存儲(chǔ)空間非常珍貴的時(shí)候處理大的數(shù)組。

Under Java, the ranges of the integer types do not depend on the machine on which you will be running the Java code. This alleviates a major pain for the programmer who wants to move software from one platform to another, or even between operating systems on the same platform. In contrast, C and C++ programs use the most efficient integer type for each processor. As a result, a C program that runs well on a 32-bit processor may exhibit integer overflow on a 16-bit system. Because Java programs must run with the same results on all machines, the ranges for the various types are fixed.

Long integer numbers have a suffix L (for example, 4000000000L). Hexadecimal numbers have a prefix 0x (for example, 0xCAFE). Octal numbers have a prefix 0. For example, 010 is 8. Naturally, this can be confusing, and we recommend against the use of octal constants.

在Java中,整數(shù)類(lèi)型的范圍不取決于你運(yùn)行Java代碼的機(jī)器。這避免了將軟件在兩個(gè)平臺(tái)或者在同一平臺(tái)上的兩個(gè)操作系統(tǒng)之間轉(zhuǎn)移而帶來(lái)的主要麻煩。與之相反的是,C和C++程序?qū)γ總€(gè)處理器使用最有效的整數(shù)類(lèi)型。造成的結(jié)果就是,一個(gè)C程序在一個(gè)32位處理器上運(yùn)行良好而在一個(gè)16位系統(tǒng)中可能表現(xiàn)出整數(shù)溢出。因?yàn)镴ava程序必須保證在所有的機(jī)器上運(yùn)行得到相同的結(jié)果,不同類(lèi)型的范圍是固定的。長(zhǎng)整型數(shù)字有一個(gè)L后綴(例如4000000000L)。十六位數(shù)字有一個(gè)0x前綴(例如,0xCAFE)。八進(jìn)制數(shù)字有一個(gè)0前綴。例如,010就是8。當(dāng)然,這可能導(dǎo)致混淆,所以我們建議避免使用八進(jìn)制數(shù)。

C++ NOTE C++注釋

?

In C and C++, int denotes the integer type that depends on the target machine. On a 16-bit processor, like the 8086, integers are 2 bytes. On a 32-bit processor like the Sun SPARC, they are 4-byte quantities. On an Intel Pentium, the integer type of C and C++ depends on the operating system: for DOS and Windows 3.1, integers are 2 bytes. When 32-bit mode is used for Windows programs, integers are 4 bytes. In Java, the sizes of all numeric types are platform independent.

Note that Java does not have any unsigned types.

在C和C++中,int表示的整數(shù)類(lèi)型取決于目標(biāo)機(jī)。在16位處理器上,例如8086機(jī),整數(shù)占用2字節(jié)。在32位處理器,例如Sun公司的SPARC,整數(shù)占用4字節(jié)。在Intel奔騰上,C和C++的整數(shù)類(lèi)型取決于操作系統(tǒng):對(duì)于DOS和Windows3.1,整數(shù)是2字節(jié)。當(dāng)Windows程序使用32位模式時(shí),整數(shù)占用4字節(jié)。在Java中,一切數(shù)字類(lèi)型的大小都是平臺(tái)無(wú)關(guān)的。注意,Java沒(méi)有任何無(wú)符號(hào)類(lèi)型。

Floating-Point Types浮點(diǎn)類(lèi)型

The floating-point types denote numbers with fractional parts. The two floating-point types are shown in Table 3-2.

浮點(diǎn)類(lèi)型表示的數(shù)字含有小數(shù)部分。表3-2顯示了兩種浮點(diǎn)類(lèi)型。

Table 3-2. Floating-Point Types

類(lèi)型

存儲(chǔ)空間

范圍

float

4字節(jié)

大約 ±3.40282347E+38F (6–7 個(gè)有效十進(jìn)制位)

double

8字節(jié)

大約 ±1.79769313486231570E+308 (15個(gè)有效十進(jìn)制位)

The name double refers to the fact that these numbers have twice the precision of the float type. (Some people call these double-precision numbers.) Here, the type to choose in most applications is double. The limited precision of float is simply not sufficient for many situations. Seven significant (decimal) digits may be enough to precisely express your annual salary in dollars and cents, but it won't be enough for your company president's salary. The only reasons to use float are in the rare situations in which the slightly faster processing of single-precision numbers is important or when you need to store a large number of them.

Numbers of type float have a suffix F (for example, 3.402F). Floating-point numbers without an F suffix (such as 3.402) are always considered to be of type double. You can optionally supply the D suffix (for example, 3.402D).

Double這個(gè)名字說(shuō)明該類(lèi)型具有兩倍于float類(lèi)型的精度。(有人將之稱(chēng)為雙精度數(shù)字。)這里,大多數(shù)應(yīng)用程序選擇double類(lèi)型。Float類(lèi)型有限的精度對(duì)于許多情況都是不足的。七個(gè)有效的十進(jìn)制位也許用來(lái)以美元和美分的單位表示你的年薪還是足夠的,但是用來(lái)表示你公司老總的薪水就顯得不夠了。僅當(dāng)極少數(shù)需要對(duì)單精度數(shù)字進(jìn)行敏捷的處理或者需要存儲(chǔ)大量單精度數(shù)字的時(shí)候,才有理由使用float類(lèi)型。

As of JDK 5.0, you can specify floating-point numbers in hexadecimal. For example, 0.125 is the same as 0x1.0p-3. In hexadecimal notation, you use a p, not an e, to denote the exponent.

從JDK5.0起,你可以指定十六進(jìn)制的浮點(diǎn)數(shù)字。例如,0.125就等同于0x1.0p-3。在十六進(jìn)制符號(hào)中,采用p而非e來(lái)表示指數(shù)。

All floating-point computations follow the IEEE 754 specification. In particular, there are three special floating-point values:

positive infinity

negative infinity

NaN (not a number)

to denote overflows and errors. For example, the result of dividing a positive number by 0 is positive infinity. Computing 0/0 or the square root of a negative number yields NaN.

所有浮點(diǎn)計(jì)算遵循IEEE 754規(guī)范。特別的,有三種特殊的浮點(diǎn)值:

positive infinity正無(wú)窮

negative infinity負(fù)無(wú)窮

NaN (非數(shù)字)

來(lái)表示溢出和錯(cuò)誤。例如,將一個(gè)正數(shù)除以0得到的結(jié)果就是正無(wú)窮。0/0或者負(fù)數(shù)的平方根就是NaN。

NOTE注釋

?

The constants Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, and Double.NaN (as well as corresponding Float constants) represent these special values, but they are rarely used in practice. In particular, you cannot test

if (x == Double.NaN) // is never true

to check whether a particular result equals Double.NaN. All "not a number" values are considered distinct. However, you can use the Double.isNaN method:

if (Double.isNaN(x)) // check whether x is "not a number"

常量Double.POSITIVE_INFINITY、Double.NEGATIVE_INFINITY、Double.NaN(以及相應(yīng)的Float常量)表示以上特殊值。但它們?cè)趯?shí)際應(yīng)用中非常少用。特別的,你無(wú)法測(cè)試

if(x==Double.NaN)//永不為真

來(lái)檢查實(shí)際結(jié)果是否等于Double.NaN。所有“非數(shù)字”值被歸為獨(dú)特的。但是你可以使用Double.isNaN方法。

if (Double.isNaN(x)) // 檢測(cè)x是否是“非數(shù)字”。

CAUTION注意

?

Floating-point numbers are not suitable for financial calculation in which roundoff errors cannot be tolerated. For example, the command System.out.println(2.0 - 1.1) prints 0.8999999999999999, not 0.9 as you would expect. Such roundoff errors are caused by the fact that floating-point numbers are represented in the binary number system. There is no precise binary representation of the fraction 1/10, just as there is no accurate representation of the fraction 1/3 in the decimal system. If you need precise numerical computations without roundoff errors, use the BigDecimal class, which is introduced later in this chapter.

浮點(diǎn)數(shù)字在不能容忍循環(huán)錯(cuò)誤的財(cái)政計(jì)算中是不合適的。例如,System.out.println(2.0 - 1.1) 命令打印出 0.8999999999999999, 而非你期望得到的0.9。這種循環(huán)錯(cuò)誤是由于在二進(jìn)制系統(tǒng)中表示浮點(diǎn)數(shù)而引起的。對(duì)于分?jǐn)?shù)1/10沒(méi)有精確的二進(jìn)制表示,正如對(duì)于分?jǐn)?shù)1/3沒(méi)有精確的十進(jìn)制表示一樣。如果你需要沒(méi)有循環(huán)錯(cuò)誤的精確數(shù)值計(jì)算,請(qǐng)使用BigDecimal類(lèi),這將在本章稍后介紹。

The char Type char類(lèi)型

To understand the char type, you have to know about the Unicode encoding scheme. Unicode was invented to overcome the limitations of traditional character encoding schemes. Before Unicode, there were many different standards: ASCII in the United States, ISO 8859-1 for Western European languages, KOI-8 for Russian, GB18030 and BIG-5 for Chinese, and so on. This causes two problems. A particular code value corresponds to different letters in the various encoding schemes. Moreover, the encodings for languages with large character sets have variable length: some common characters are encoded as single bytes, others require two or more bytes.

要理解char類(lèi)型,你需要了解Unicode編碼規(guī)則。Unicode是為克服傳統(tǒng)字符編碼規(guī)則的局限性而發(fā)明的。在Unicode之前,有許多標(biāo)準(zhǔn):美國(guó)的ASCII、西歐語(yǔ)言的ISO 8859-1、俄語(yǔ)使用的KOI-8、中文使用的GB18030 和 BIG-5等等。這造成了兩個(gè)問(wèn)題。一個(gè)特定的碼值在不同的編碼規(guī)則中對(duì)應(yīng)于不同的字母。此外,大字符集的語(yǔ)言使用的編碼具有可變的長(zhǎng)度:一些普通的字符以單字節(jié)編碼,而其余的需要兩個(gè)或更多字節(jié)。

Unicode was designed to solve these problems. When the unification effort started in the 1980s, a fixed 2-byte width code was more than sufficient to encode all characters used in all languages in the world, with room to spare for future expansion—or so everyone thought at the time. In 1991, Unicode 1.0 was released, using slightly less than half of the available 65,536 code values. Java was designed from the ground up to use 16-bit Unicode characters, which was a major advance over other programming languages that used 8-bit characters.

Unicode的初衷就是解決這個(gè)問(wèn)題。在統(tǒng)一化進(jìn)程始于20世紀(jì)80年代的時(shí)候,一個(gè)定長(zhǎng)的兩字節(jié)碼已經(jīng)足夠?qū)κ澜缟纤姓Z(yǔ)言中的所有字符進(jìn)行編碼,剩下的空間還可以用于將來(lái)的擴(kuò)展——大概當(dāng)初每個(gè)人都是這樣認(rèn)為的。1991年,Unicode 1.0誕生了,僅使用了全部可用65536個(gè)碼值中的一半。Java被設(shè)計(jì)為完全采用16位Unicode字符,這是Java優(yōu)于其他采用8位字符的一個(gè)主要優(yōu)點(diǎn)。

Unfortunately, over time, the inevitable happened. Unicode grew beyond 65,536 characters, primarily due to the addition of a very large set of ideographs used for Chinese, Japanese, and Korean. Now, the 16-bit char type is insufficient to describe all Unicode characters.

We need a bit of terminology to explain how this problem is resolved in Java, beginning with JDK 5.0. A code point is a code value that is associated with a character in an encoding scheme. In the Unicode standard, code points are written in hexadecimal and prefixed with U+, such as U+0041 for the code point of the letter A. Unicode has code points that are grouped into 17 code planes. The first code plane, called the basic multilingual plane, consists of the "classic" Unicode characters with code points U+0000 to U+FFFF. Sixteen additional planes, with code points U+10000 to U+10FFFF, hold the supplementary characters.

不幸的是,隨著時(shí)間的過(guò)去,不可避免的事情發(fā)生了。Unicode超過(guò)了65535個(gè)字符,主要是由于像中文、日文、韓文這樣的大量的象形文字的加入而造成的。現(xiàn)在,16位char類(lèi)型已經(jīng)不足以描述所有Unicode字符。

我們需要使用一點(diǎn)術(shù)語(yǔ)來(lái)解釋這個(gè)問(wèn)題在Java中,從JDK5.0開(kāi)始是如何得以解決的。一個(gè)代碼點(diǎn)就是一個(gè)編碼規(guī)則中與一個(gè)字符相關(guān)聯(lián)的碼值。在Unicode標(biāo)準(zhǔn)中,代碼點(diǎn)是用16進(jìn)制寫(xiě)成,加上U+前綴,例如U+0041就是字母A的代碼點(diǎn)。Unicode的代碼點(diǎn)被分成17個(gè)代碼組。第一個(gè)代碼組叫做基本多語(yǔ)言組,是由采用U+0000至U+FFFF代碼點(diǎn)的“經(jīng)典”Unicode字符組成。額外的16個(gè)代碼組,代碼點(diǎn)從U+10000至U+10FFFF,保存輔助字符。

The UTF-16 encoding is a method of representing all Unicode code points in a variable length code. The characters in the basic multilingual plane are represented as 16-bit values, called code units. The supplementary characters are encoded as consecutive pairs of code units. Each of the values in such an encoding pair falls into an unused 2048-byte range of the basic multilingual plane, called the surrogates area (U+D800 to U+DBFF for the first code unit, U+DC00 to U+DFFF for the second code unit).This is rather clever, because you can immediately tell whether a code unit encodes a single character or whether it is the first or second part of a supplementary character. For example, the mathematical symbol for the set of integers has code point U+1D56B and is encoded by the two code units U+D835 and U+DD6B. (See http://en.wikipedia.org/wiki/UTF-16 for a description of the encoding algorithm.)

UTF-16編碼是一種以變長(zhǎng)編碼表示所有Unicode代碼點(diǎn)的方法。基本多語(yǔ)言組中的字符以16位值表示,叫做代碼單元。輔助字符以連續(xù)的代碼單元對(duì)編碼。這樣一個(gè)編碼對(duì)中的每個(gè)值就屬于一個(gè)未占用的2048字節(jié)的基本多語(yǔ)言組范圍,稱(chēng)作代理區(qū)域(U+D800至U+DBFF 表示第一個(gè)代碼單元, U+DC00至U+DFFF表示第二個(gè)代碼單元)。這是相當(dāng)明智的,因?yàn)槟憧梢粤⒓凑f(shuō)出一個(gè)代碼單元是否對(duì)一個(gè)單字符進(jìn)行編碼或者它是輔助字符的第一部分還是第二部分。例如,整數(shù)集的算術(shù)符號(hào)的代碼點(diǎn)為U+1D56B,它是由兩個(gè)代碼單元U+D835和U+DD6B編碼而成。(參見(jiàn)http://en.wikipedia.org/wiki/UTF-16獲得有關(guān)編碼算法的描述)

In Java, the char type describes a code unit in the UTF-16 encoding.

Our strong recommendation is not to use the char type in your programs unless you are actually manipulating UTF-16 code units. You are almost always better off treating strings as abstract data types.

Java中,char類(lèi)型描述UTF-16編碼中的一個(gè)代碼單元。

我們強(qiáng)烈建議在程序中避免使用char類(lèi)型,除非你對(duì)UTF-16代碼單元十分熟練。你幾乎總是將字符串視為抽象數(shù)據(jù)類(lèi)型即可。

Having said that, there will be some cases when you will encounter char values. Most commonly, these will be character constants. For example, 'A' is a character constant with value 65. It is different from "A", a string containing a single character. Unicode code units can be expressed as hexadecimal values that run from \u0000 to \uFFFF. For example, \u2122 is the trademark symbol (?) and \u03C0 is the Greek letter pi (p).

盡管如上所述,但是有時(shí)候你也會(huì)遇到char值。最常見(jiàn)的就是字符常量。例如,‘A’是一個(gè)值為65的字符常量。它與“A”這個(gè)僅包含一個(gè)字符的字符串不同。Unicode代碼單元可被表示成從\u0000到\uFFFF的十六進(jìn)制值。例如,\u2122是商標(biāo)符號(hào)(?) 而 \u03C0 是希臘字母 (p).

Besides the \u escape sequences that indicate the encoding of Unicode code units, there are several escape sequences for special characters, as shown in Table 3-3. You can use these escape sequences inside quoted character constants and strings, such as '\u2122' or "Hello\n". The \u escape sequence (but none of the other escape sequences) can even be used outside quoted character constants and strings. For example,

public static void main(String\5B\5D args)

除了用\u轉(zhuǎn)義符來(lái)表示Unicode代碼單元的編碼,還有一些特殊的轉(zhuǎn)義符來(lái)表示特殊字符,如表3-3所示。你可以在用引號(hào)引起來(lái)的字符常量和字符串中使用這些轉(zhuǎn)義符,例如'\u2122'或"Hello\n"。\u轉(zhuǎn)義符(但其他轉(zhuǎn)義符除外)甚至可以在引號(hào)引起來(lái)的字符常量和字符串外使用。例如:public static void main(String\5B\5D args)

Table 3-3. Escape Sequences for Special Characters

Escape Sequence

Name

Unicode Value

\b

退格

\u0008

\t

Tab

\u0009

\n

換行

\u000a

\r

回車(chē)

\u000d

\"

雙引號(hào)

\u0022

\'

單引號(hào)

\u0027

\\

反斜線(xiàn)

\u005c

is perfectly legal—\u005B and \u005D are the UTF-16 encodings of the Unicode code points for [ and ].

是完全合法的——\u005B和\u005D 是“[”和“]”的Unicode代碼點(diǎn)的UTF-16編碼。

NOTE注釋

?

Although you can use any Unicode character in a Java application or applet, whether you can actually see it displayed depends on your browser (for applets) and (ultimately) on your operating system for both.

盡管你可以在任何Java程序和Applet中使用Unicode字符,但實(shí)際上你能否看到這些字符還要取決于你的瀏覽器(對(duì)Applet而言)和你的操作系統(tǒng)(這是最基本的)。

The boolean Type boolean類(lèi)型(也作布爾類(lèi)型)

The boolean type has two values, false and true. It is used for evaluating logical conditions. You cannot convert between integers and boolean values.

boolean類(lèi)型有兩個(gè)值,false和true。這是用來(lái)判斷邏輯條件的。你不能在整型和布爾型之間進(jìn)行轉(zhuǎn)換。

C++ NOTE C++注釋

?

In C++, numbers and even pointers can be used in place of boolean values. The value 0 is equivalent to the bool value false, and a non-zero value is equivalent to true. This is not the case in Java. Thus, Java programmers are shielded from accidents such as

if (x = 0) // oops...meant x == 0

In C++, this test compiles and runs, always evaluating to false. In Java, the test does not compile because the integer expression x = 0 cannot be converted to a boolean value.

在C++中,數(shù)字乃至小數(shù)點(diǎn)都被用于替代布爾值。0值就相當(dāng)于布爾值false而非0值相當(dāng)于true。Java中并非如此。因此Java程序員不會(huì)遇到下面的情況:

if (x = 0) // 哇。。。意味著x==0

C++中,這個(gè)測(cè)試可以編譯并運(yùn)行,并且總是判斷為false。在Java中,這個(gè)測(cè)試無(wú)法編譯,因?yàn)檎捅磉_(dá)式x=0不能被轉(zhuǎn)換為布爾值。


文章來(lái)源:http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!289.entry