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是一種強類型語言。這意味著每個變量必須聲明類型。Java中有八種原始類型。其中4種是整數(shù)類型;兩種是浮點數(shù)類型;一種是字符類型char,適用于Unicode編碼方案中的代碼單元(參見本節(jié)中的char類型小節(jié));還有一種是用來表示真值的布爾類型。
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有一個任意精度的算術(shù)包。顧名思義,“大數(shù)”是一個Java對象,而并非新的Java類型。你將在本章后面看到如何使用它們。
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ù)類型是四種無小數(shù)部分的數(shù)字,允許負值。Java提供表3-1所示的四種整數(shù)類型。
Table 3-1. Java Integer Types
類型
存儲空間
范圍(閉區(qū)間)
Int
4 字節(jié)
–2,147,483,648 to 2,147,483, 647 (超過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類型是最實用的。如果你想表示我們星球上居民的數(shù)量,你就需要訴諸于long類型。Byte和short類型主要用于一些特殊的應用,比如較低級的文件處理,或者當存儲空間非常珍貴的時候處理大的數(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ù)類型的范圍不取決于你運行Java代碼的機器。這避免了將軟件在兩個平臺或者在同一平臺上的兩個操作系統(tǒng)之間轉(zhuǎn)移而帶來的主要麻煩。與之相反的是,C和C++程序?qū)γ總€處理器使用最有效的整數(shù)類型。造成的結(jié)果就是,一個C程序在一個32位處理器上運行良好而在一個16位系統(tǒng)中可能表現(xiàn)出整數(shù)溢出。因為Java程序必須保證在所有的機器上運行得到相同的結(jié)果,不同類型的范圍是固定的。長整型數(shù)字有一個L后綴(例如4000000000L)。十六位數(shù)字有一個0x前綴(例如,0xCAFE)。八進制數(shù)字有一個0前綴。例如,010就是8。當然,這可能導致混淆,所以我們建議避免使用八進制數(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ù)類型取決于目標機。在16位處理器上,例如8086機,整數(shù)占用2字節(jié)。在32位處理器,例如Sun公司的SPARC,整數(shù)占用4字節(jié)。在Intel奔騰上,C和C++的整數(shù)類型取決于操作系統(tǒng):對于DOS和Windows3.1,整數(shù)是2字節(jié)。當Windows程序使用32位模式時,整數(shù)占用4字節(jié)。在Java中,一切數(shù)字類型的大小都是平臺無關(guān)的。注意,Java沒有任何無符號類型。
Floating-Point Types浮點類型
The floating-point types denote numbers with fractional parts. The two floating-point types are shown in Table 3-2.
浮點類型表示的數(shù)字含有小數(shù)部分。表3-2顯示了兩種浮點類型。
Table 3-2. Floating-Point Types
類型
存儲空間
范圍
float
4字節(jié)
大約 ±3.40282347E+38F (6–7 個有效十進制位)
double
8字節(jié)
大約 ±1.79769313486231570E+308 (15個有效十進制位)
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這個名字說明該類型具有兩倍于float類型的精度。(有人將之稱為雙精度數(shù)字。)這里,大多數(shù)應用程序選擇double類型。Float類型有限的精度對于許多情況都是不足的。七個有效的十進制位也許用來以美元和美分的單位表示你的年薪還是足夠的,但是用來表示你公司老總的薪水就顯得不夠了。僅當極少數(shù)需要對單精度數(shù)字進行敏捷的處理或者需要存儲大量單精度數(shù)字的時候,才有理由使用float類型。
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起,你可以指定十六進制的浮點數(shù)字。例如,0.125就等同于0x1.0p-3。在十六進制符號中,采用p而非e來表示指數(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.
所有浮點計算遵循IEEE 754規(guī)范。特別的,有三種特殊的浮點值:
positive infinity正無窮
negative infinity負無窮
NaN (非數(shù)字)
來表示溢出和錯誤。例如,將一個正數(shù)除以0得到的結(jié)果就是正無窮。0/0或者負數(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(以及相應的Float常量)表示以上特殊值。但它們在實際應用中非常少用。特別的,你無法測試
if(x==Double.NaN)//永不為真
來檢查實際結(jié)果是否等于Double.NaN。所有“非數(shù)字”值被歸為獨特的。但是你可以使用Double.isNaN方法。
if (Double.isNaN(x)) // 檢測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.
浮點數(shù)字在不能容忍循環(huán)錯誤的財政計算中是不合適的。例如,System.out.println(2.0 - 1.1) 命令打印出 0.8999999999999999, 而非你期望得到的0.9。這種循環(huán)錯誤是由于在二進制系統(tǒng)中表示浮點數(shù)而引起的。對于分數(shù)1/10沒有精確的二進制表示,正如對于分數(shù)1/3沒有精確的十進制表示一樣。如果你需要沒有循環(huán)錯誤的精確數(shù)值計算,請使用BigDecimal類,這將在本章稍后介紹。
The char Type char類型
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類型,你需要了解Unicode編碼規(guī)則。Unicode是為克服傳統(tǒng)字符編碼規(guī)則的局限性而發(fā)明的。在Unicode之前,有許多標準:美國的ASCII、西歐語言的ISO 8859-1、俄語使用的KOI-8、中文使用的GB18030 和 BIG-5等等。這造成了兩個問題。一個特定的碼值在不同的編碼規(guī)則中對應于不同的字母。此外,大字符集的語言使用的編碼具有可變的長度:一些普通的字符以單字節(jié)編碼,而其余的需要兩個或更多字節(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的初衷就是解決這個問題。在統(tǒng)一化進程始于20世紀80年代的時候,一個定長的兩字節(jié)碼已經(jīng)足夠?qū)κ澜缟纤姓Z言中的所有字符進行編碼,剩下的空間還可以用于將來的擴展——大概當初每個人都是這樣認為的。1991年,Unicode 1.0誕生了,僅使用了全部可用65536個碼值中的一半。Java被設(shè)計為完全采用16位Unicode字符,這是Java優(yōu)于其他采用8位字符的一個主要優(yōu)點。
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.
不幸的是,隨著時間的過去,不可避免的事情發(fā)生了。Unicode超過了65535個字符,主要是由于像中文、日文、韓文這樣的大量的象形文字的加入而造成的。現(xiàn)在,16位char類型已經(jīng)不足以描述所有Unicode字符。
我們需要使用一點術(shù)語來解釋這個問題在Java中,從JDK5.0開始是如何得以解決的。一個代碼點就是一個編碼規(guī)則中與一個字符相關(guān)聯(lián)的碼值。在Unicode標準中,代碼點是用16進制寫成,加上U+前綴,例如U+0041就是字母A的代碼點。Unicode的代碼點被分成17個代碼組。第一個代碼組叫做基本多語言組,是由采用U+0000至U+FFFF代碼點的“經(jīng)典”Unicode字符組成。額外的16個代碼組,代碼點從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編碼是一種以變長編碼表示所有Unicode代碼點的方法。基本多語言組中的字符以16位值表示,叫做代碼單元。輔助字符以連續(xù)的代碼單元對編碼。這樣一個編碼對中的每個值就屬于一個未占用的2048字節(jié)的基本多語言組范圍,稱作代理區(qū)域(U+D800至U+DBFF 表示第一個代碼單元, U+DC00至U+DFFF表示第二個代碼單元)。這是相當明智的,因為你可以立即說出一個代碼單元是否對一個單字符進行編碼或者它是輔助字符的第一部分還是第二部分。例如,整數(shù)集的算術(shù)符號的代碼點為U+1D56B,它是由兩個代碼單元U+D835和U+DD6B編碼而成。(參見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類型描述UTF-16編碼中的一個代碼單元。
我們強烈建議在程序中避免使用char類型,除非你對UTF-16代碼單元十分熟練。你幾乎總是將字符串視為抽象數(shù)據(jù)類型即可。
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).
盡管如上所述,但是有時候你也會遇到char值。最常見的就是字符常量。例如,‘A’是一個值為65的字符常量。它與“A”這個僅包含一個字符的字符串不同。Unicode代碼單元可被表示成從\u0000到\uFFFF的十六進制值。例如,\u2122是商標符號(?) 而 \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)義符來表示Unicode代碼單元的編碼,還有一些特殊的轉(zhuǎn)義符來表示特殊字符,如表3-3所示。你可以在用引號引起來的字符常量和字符串中使用這些轉(zhuǎn)義符,例如'\u2122'或"Hello\n"。\u轉(zhuǎn)義符(但其他轉(zhuǎn)義符除外)甚至可以在引號引起來的字符常量和字符串外使用。例如: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
回車
\u000d
\"
雙引號
\u0022
\'
單引號
\u0027
\\
反斜線
\u005c
is perfectly legal—\u005B and \u005D are the UTF-16 encodings of the Unicode code points for [ and ].
是完全合法的——\u005B和\u005D 是“[”和“]”的Unicode代碼點的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字符,但實際上你能否看到這些字符還要取決于你的瀏覽器(對Applet而言)和你的操作系統(tǒng)(這是最基本的)。
The boolean Type boolean類型(也作布爾類型)
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類型有兩個值,false和true。這是用來判斷邏輯條件的。你不能在整型和布爾型之間進行轉(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ù)點都被用于替代布爾值。0值就相當于布爾值false而非0值相當于true。Java中并非如此。因此Java程序員不會遇到下面的情況:
if (x = 0) // 哇。。。意味著x==0
C++中,這個測試可以編譯并運行,并且總是判斷為false。在Java中,這個測試無法編譯,因為整型表達式x=0不能被轉(zhuǎn)換為布爾值。
文章來源:
http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!289.entry