文章作者:Tyan
博客:noahsnail.com
Chapter 1 Introduction
THIS book is designed to help you make the most effective use of the JavaTM programming language and its fundamental libraries, java.lang
, java.util
, and, to a lesser extent, java.util.concurrent
and java.io
. The book discusses other libraries from time to time, but it does not cover graphical user interface programming, enterprise APIs, or mobile devices.
本書的目的是為了幫助你最有效的利用Java編程語言和它的基礎(chǔ)庫,java.lang
,java.util
之景,在更小程度上包括java.util.concurrent
和java.io
。本書有時(shí)會(huì)討論其它的庫葵诈,但不包括圖形用戶接口編程,企業(yè)APIs或移動(dòng)設(shè)備。
This book consists of seventy-eight items, each of which conveys one rule. The rules capture practices generally held to be beneficial by the best and most experienced programmers. The items are loosely grouped into ten chapters, each concerning one broad aspect of software design. The book is not intended to be read from cover to cover: each item stands on its own, more or less. The items are heavily cross-referenced so you can easily plot your own course through the book.
本書包括七十八個(gè)條目驯击,每個(gè)條目傳達(dá)一條規(guī)則烁兰。這些規(guī)則通常是從實(shí)踐中得到并且最好最有經(jīng)驗(yàn)的程序員堅(jiān)信它是有益的。這些條目被松散的分為十章徊都,每章都是關(guān)于軟件設(shè)計(jì)方面的一個(gè)擴(kuò)展沪斟。本書不打算被從頭到尾的讀,每個(gè)條目或多或少都是依賴于它本身暇矫。這些條目之間的交叉引用非常嚴(yán)重主之,因此你可以很容易的通過本書劃分自己的進(jìn)度。
Many new features were added to the platform in Java 5 (release 1.5). Most of the items in this book use these features in some way. The following table shows you where to go for primary coverage of these features:
Java 5平臺(tái)增加了許多新功能李根。本書中的大多數(shù)條目在某種程度上使用了這些功能槽奕。下表列出了這些新功能在本書中的位置:
Most items are illustrated with program examples. A key feature of this book is that it contains code examples illustrating many design patterns and idioms. Where appropriate, they are cross-referenced to the standard reference work in this area [Gamma95].
大多數(shù)條目通過程序?qū)嵗M(jìn)行說明。本書的一個(gè)重要特點(diǎn)是它包含了說明許多設(shè)計(jì)模式和習(xí)慣用法的代碼實(shí)例房轿。這些條目放在哪里是合適的粤攒,它們被交叉參考引用到了這個(gè)領(lǐng)域的標(biāo)準(zhǔn)參考著作[Gamma 95]。
Many items contain one or more program examples illustrating some practice to be avoided. Such examples, sometimes known as antipatterns, are clearly labeled with a comment such as “// Never do this!” In each case, the item explains why the example is bad and suggests an alternative approach.
許多條目包含一個(gè)或多個(gè)用來表明一些應(yīng)該在實(shí)踐中避免的程序?qū)嵗殉帧_@些例子中的都加上了清楚的注釋例如“// Never do this!”夯接,有時(shí)候這些例子也被稱為反模式。在每一個(gè)例子中纷妆,這個(gè)條目都解釋了為什么這個(gè)例子是不好的盔几,并且提建議了一種可替代方法。
This book is not for beginners: it assumes that you are already comfortable with the Java programming language. If you are not, consider one of the many fine introductory texts [Arnold05, Sestoft05]. While the book is designed to be accessible to anyone with a working knowledge of the language, it should provide food for thought even for advanced programmers.
本書不是給初學(xué)者的:它假定你已經(jīng)非常熟悉Java編程語言掩幢。如果你對(duì)Java語言不熟悉逊拍,請(qǐng)考慮許多很好的入門書籍中的一本[Arnold05, Sestoft05]。雖然本書的目標(biāo)是任何具有實(shí)際Java編程經(jīng)驗(yàn)的人际邻,但它應(yīng)該能提供一些思考的東西芯丧,即使是對(duì)于高級(jí)程序員。
Most of the rules in this book derive from a few fundamental principles. Clarity and simplicity are of paramount importance. The user of a module should never be surprised by its behavior. Modules should be as small as possible but no smaller. (As used in this book, the term module refers to any reusable software component, from an individual method to a complex system consisting of multiple packages.) Code should be reused rather than copied. The dependencies between modules should be kept to a minimum. Errors should be detected as soon as possible after they are made, ideally at compile time.
本書中的大多數(shù)規(guī)則源于一些基本的原則枯怖。簡潔清晰是最重要的注整。模塊的用戶不應(yīng)該對(duì)它的行為感到驚奇能曾。模塊要盡可能的小但不是更小度硝。(本書中使用的術(shù)語模塊指的是任何可重用的軟件組件,從單個(gè)方法到由多個(gè)包組成的復(fù)雜系統(tǒng))寿冕。代碼應(yīng)該被重用而不是拷貝蕊程。模塊間的依賴性要保持最小。錯(cuò)誤應(yīng)該盡早檢測出來驼唱,理想情況是在編譯時(shí)發(fā)現(xiàn)藻茂。
While the rules in this book do not apply 100 percent of the time, they do characterize best programming practices in the great majority of cases. You should not slavishly follow these rules, but violate them only occasionally and with good reason. Learning the art of programming, like most other disciplines, consists of first learning the rules and then learning when to break them.
雖然本書中的規(guī)則不能百分百的應(yīng)用于任何時(shí)間,但在大多數(shù)情況下具有最好編程實(shí)踐的特征。你不應(yīng)該盲從這些規(guī)則辨赐,但只是偶爾在有充足的理由的時(shí)候才違反這些規(guī)則优俘。像大多數(shù)其它學(xué)科一樣,學(xué)習(xí)編程藝術(shù)包括首先學(xué)習(xí)規(guī)則掀序,然后學(xué)習(xí)在什么時(shí)候打破規(guī)則帆焕。
For the most part, this book is not about performance. It is about writing programs that are clear, correct, usable, robust, flexible, and maintainable. If you can do that, it’s usually a relatively simple matter to get the performance you need (Item 55). Some items do discuss performance concerns, and a few of these items provide performance numbers. These numbers, which are introduced with the phrase “On my machine,” should be regarded as approximate at best.
本書的大部分不是關(guān)于性能的。它是關(guān)于編寫清晰不恭、正確叶雹、可用、魯棒换吧、有彈性并且可維護(hù)的程序的折晦。如果你能做到這一點(diǎn),要得到你需要的性能它通常是相對(duì)簡單的(條目55)沾瓦。一些條目討論性能的關(guān)注點(diǎn)满着,這些條目中的一些提供了性能指數(shù)。這些指數(shù)應(yīng)該被看做與最好情況下近似贯莺,這些指數(shù)介紹時(shí)使用了詞語"在我的機(jī)器上"漓滔。
For what it’s worth, my machine is an aging homebuilt 2.2 GHz dual-core AMD Opteron<top>TM</top> 170 with 2 gigabytes of RAM, running Sun’s 1.6_05 release of the Java SE Development Kit (JDK) atop Microsoft Windows? XP Professional SP2. This JDK has two virtual machines, the Java HotSpot<top>TM</top> Client and Server VMs. Performance numbers were measured on the Server VM.
值得注意的是,我的機(jī)器是老舊的組裝電腦乖篷,2.2G赫茲雙核AMD 皓龍?zhí)幚砥?170响驴,2G內(nèi)存,在微軟的Windows XP SP2上運(yùn)行Sun的JDK 1.6_05版本撕蔼。JDK有兩個(gè)虛擬機(jī)豁鲤,Java熱交換客戶端和服務(wù)器虛擬機(jī)。性能指標(biāo)是在服務(wù)器虛擬機(jī)上測量的鲸沮。
When discussing features of the Java programming language and its libraries, it is sometimes necessary to refer to specific releases. For brevity, this book uses “engineering version numbers” in preference to official release names. This table shows the mapping between release names and engineering version numbers.
當(dāng)討論Java編程語言的特性和它的庫時(shí)琳骡,有時(shí)指明特定的版本是必要的。為了簡潔讼溺,本書使用工程版本號(hào)而不是正式的發(fā)行名稱楣号。下表顯示了發(fā)行名稱與工程版本號(hào)的映射關(guān)系。
The examples are reasonably complete, but they favor readability over completeness. They freely use classes from the packages java.util
and java.io
. In order to compile the examples, you may have to add one or more of these import statements:
雖然這些例子是相當(dāng)完整的怒坯,但它們注重可讀性甚于完整性炫狱。他們可以很自由的使用包java.util
和java.io
中的類。為了編譯這些例子剔猿,你可能必須添加一個(gè)或多個(gè)導(dǎo)入聲明:
import java.util.*;
import java.util.concurrent.*;
import java.io.*;
Other boilerplate is similarly omitted. The book’s Web site, http://java.sun.com/docs/books/effective, contains an expanded version of each example, which you can compile and run.
其它的例子中也有類似的省略情況视译。本書的網(wǎng)站:http://java.sun.com/docs/books/effective,含有每個(gè)例子的擴(kuò)展版本归敬,你可以編譯并且運(yùn)行鄙早。
For the most part, this book uses technical terms as they are defined in The Java Language Specification, Third Edition [JLS]. A few terms deserve special mention. The language supports four kinds of types: interfaces (including annotations), classes (including enums), arrays, and primitives. The first three are known as reference types. Class instances and arrays are objects; primitive values are not. A class’s members consist of its fields, methods, member classes, and member interfaces. A method’s signature consists of its name and the types of its formal parameters; the signature does not include the method’s return type.
本書中的大部分技術(shù)術(shù)語與Java語言規(guī)范(第三版)中的術(shù)語是一樣的椅亚。一些術(shù)語需要特別指出。Java語言支持四種類型:接口(包括注解)呀舔,類(包括枚舉)扳缕,數(shù)組和基本類型。前三個(gè)是引用類型躯舔。類實(shí)例和數(shù)組是對(duì)象省古,基本類型不是粥庄。類成員由它的域、方法豺妓、成員類和成員接口組成惜互。方法的簽名由它的名字、正式的參數(shù)類型組成训堆;簽名不包括方法的返回值類型白嘁。
This book uses a few terms differently from the The Java Language Specification. Unlike The Java Language Specification, this book uses inheritance as a synonym for subclassing. Instead of using the term inheritance for interfaces, this book simply states that a class implements an interface or that one interface extends another. To describe the access level that applies when none is specified, this book uses the descriptive term package-private instead of the technically correct term default access [JLS, 6.6.1].
本書使用了一些與Java語言規(guī)范不同的術(shù)語。不像Java語言規(guī)范鲁沥,本書使用繼承作為子類的同義詞耕魄。不再使用接口繼承的術(shù)語画恰,本書簡單表述一個(gè)類實(shí)現(xiàn)了一個(gè)接口或一個(gè)接口擴(kuò)展了另一個(gè)接口允扇。為了描述沒有指定訪問級(jí)別的情況则奥,本書使用描述術(shù)語包私有代替技術(shù)上正確的術(shù)語缺省訪問[JLS, 6.6.1].
This book uses a few technical terms that are not defined in The Java Language Specification. The term exported API, or simply API, refers to the classes, interfaces, constructors, members, and serialized forms by which a programmer accesses a class, interface, or package. (The term API, which is short for application programming interface, is used in preference to the otherwise preferable term interface to avoid confusion with the language construct of that name.) A programmer who writes a program that uses an API is referred to as a user of the API. A class whose implementation uses an API is a client of the API.
本書使用一些Java語言規(guī)范沒有定義的術(shù)語。術(shù)語exported API
或simply API
额划,指的是類档泽、接口、構(gòu)造函數(shù)馆匿、成員、序列化形式阿逃,程序員通過它們?cè)L問類赃蛛、接口或包。(術(shù)語API呕臂,是應(yīng)用程序接口的縮寫,優(yōu)先使用API而不是其他人更喜歡的術(shù)語接口土砂,是為了避免與Java語言中的接口相混淆谜洽。)程序員寫程序使用API指的是API的用戶。類中實(shí)現(xiàn)使用了API的稱為API的客戶阐虚。
Classes, interfaces, constructors, members, and serialized forms are collectively known as API elements. An exported API consists of the API elements that are accessible outside of the package that defines the API. These are the API elements that any client can use and the author of the API commits to support. Not coincidentally, they are also the elements for which the Javadoc utility generates documentation in its default mode of operation. Loosely speaking, the exported API of a package consists of the public and protected members and constructors of every public class or interface in the package.
類、接口实束、構(gòu)造函數(shù)、成員和序列化形式統(tǒng)稱為API元素吭练。導(dǎo)出API由定義API的包的包外能訪問的API元素組成析显。這些API元素是任何客戶都能使用的并且API的作者提供支持。無獨(dú)有偶分尸,Java工具類默認(rèn)操作模式下也為這些元素產(chǎn)生了文檔歹嘹。不嚴(yán)格的說,包的導(dǎo)出API由公有成員尺上、保護(hù)成員和每個(gè)公有類的構(gòu)造函數(shù)或包中的接口組成圆到。