Effective Java 2.0_中英文對(duì)照_第一章

文章作者: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.langjava.util之景,在更小程度上包括java.util.concurrentjava.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ù)條目在某種程度上使用了這些功能槽奕。下表列出了這些新功能在本書中的位置:

image

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)系。

image

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.utiljava.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 APIsimply 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ù)或包中的接口組成圆到。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末芽淡,一起剝皮案震驚了整個(gè)濱河市豆赏,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌掷邦,老刑警劉巖抚岗,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異苟跪,居然都是意外死亡件已,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門兄猩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鉴未,“玉大人,你說我怎么就攤上這事铜秆。” “怎么了核蘸?”我有些...
    開封第一講書人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵啸驯,是天一觀的道長。 經(jīng)常有香客問我徙鱼,道長针姿,這世上最難降的妖魔是什么厌衙? 我笑而不...
    開封第一講書人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任杆故,我火速辦了婚禮溉愁,結(jié)果婚禮上拐揭,老公的妹妹穿的比我還像新娘撤蟆。我一直安慰自己堂污,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開白布讨衣。 她就那樣靜靜地躺著反镇,像睡著了一般娘汞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上你弦,一...
    開封第一講書人閱讀 51,462評(píng)論 1 302
  • 那天禽作,我揣著相機(jī)與錄音,去河邊找鬼旷偿。 笑死,一個(gè)胖子當(dāng)著我的面吹牛衷蜓,可吹牛的內(nèi)容都是我干的尘喝。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼置吓,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了衍锚?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤度宦,失蹤者是張志新(化名)和其女友劉穎告匠,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體划鸽,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡戚哎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年型凳,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片啰脚。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡橄浓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出荸实,到底是詐尸還是另有隱情,我是刑警寧澤泄朴,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布露氮,位于F島的核電站,受9級(jí)特大地震影響局扶,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜三妈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一畴蒲、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧模燥,春花似錦、人聲如沸心肪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至锅减,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間握联,已是汗流浹背每瞒。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留代芜,地道東北人浓利。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像嫡秕,于是被迫代替她去往敵國和親苹威。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容