Effective Java 2.0_中英文對照_第一章

文章作者: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編程語言和它的基礎庫,java.langjava.util,在更小程度上包括java.util.concurrentjava.io。本書有時會討論其它的庫,但不包括圖形用戶接口編程,企業APIs或移動設備。

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.

本書包括七十八個條目,每個條目傳達一條規則。這些規則通常是從實踐中得到并且最好最有經驗的程序員堅信它是有益的。這些條目被松散的分為十章,每章都是關于軟件設計方面的一個擴展。本書不打算被從頭到尾的讀,每個條目或多或少都是依賴于它本身。這些條目之間的交叉引用非常嚴重,因此你可以很容易的通過本書劃分自己的進度。

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平臺增加了許多新功能。本書中的大多數條目在某種程度上使用了這些功能。下表列出了這些新功能在本書中的位置:

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].

大多數條目通過程序實例進行說明。本書的一個重要特點是它包含了說明許多設計模式和習慣用法的代碼實例。這些條目放在哪里是合適的,它們被交叉參考引用到了這個領域的標準參考著作[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.

許多條目包含一個或多個用來表明一些應該在實踐中避免的程序實例。這些例子中的都加上了清楚的注釋例如“// Never do this!”,有時候這些例子也被稱為反模式。在每一個例子中,這個條目都解釋了為什么這個例子是不好的,并且提建議了一種可替代方法。

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.

本書不是給初學者的:它假定你已經非常熟悉Java編程語言。如果你對Java語言不熟悉,請考慮許多很好的入門書籍中的一本[Arnold05, Sestoft05]。雖然本書的目標是任何具有實際Java編程經驗的人,但它應該能提供一些思考的東西,即使是對于高級程序員。

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.

本書中的大多數規則源于一些基本的原則。簡潔清晰是最重要的。模塊的用戶不應該對它的行為感到驚奇。模塊要盡可能的小但不是更小。(本書中使用的術語模塊指的是任何可重用的軟件組件,從單個方法到由多個包組成的復雜系統)。代碼應該被重用而不是拷貝。模塊間的依賴性要保持最小。錯誤應該盡早檢測出來,理想情況是在編譯時發現。

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.

雖然本書中的規則不能百分百的應用于任何時間,但在大多數情況下具有最好編程實踐的特征。你不應該盲從這些規則,但只是偶爾在有充足的理由的時候才違反這些規則。像大多數其它學科一樣,學習編程藝術包括首先學習規則,然后學習在什么時候打破規則。

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.

本書的大部分不是關于性能的。它是關于編寫清晰、正確、可用、魯棒、有彈性并且可維護的程序的。如果你能做到這一點,要得到你需要的性能它通常是相對簡單的(條目55)。一些條目討論性能的關注點,這些條目中的一些提供了性能指數。這些指數應該被看做與最好情況下近似,這些指數介紹時使用了詞語"在我的機器上"。

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.

值得注意的是,我的機器是老舊的組裝電腦,2.2G赫茲雙核AMD 皓龍處理器 170,2G內存,在微軟的Windows XP SP2上運行Sun的JDK 1.6_05版本。JDK有兩個虛擬機,Java熱交換客戶端和服務器虛擬機。性能指標是在服務器虛擬機上測量的。

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.

當討論Java編程語言的特性和它的庫時,有時指明特定的版本是必要的。為了簡潔,本書使用工程版本號而不是正式的發行名稱。下表顯示了發行名稱與工程版本號的映射關系。

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:

雖然這些例子是相當完整的,但它們注重可讀性甚于完整性。他們可以很自由的使用包java.utiljava.io中的類。為了編譯這些例子,你可能必須添加一個或多個導入聲明:

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.

其它的例子中也有類似的省略情況。本書的網站:http://java.sun.com/docs/books/effective,含有每個例子的擴展版本,你可以編譯并且運行。

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.

本書中的大部分技術術語與Java語言規范(第三版)中的術語是一樣的。一些術語需要特別指出。Java語言支持四種類型:接口(包括注解),類(包括枚舉),數組和基本類型。前三個是引用類型。類實例和數組是對象,基本類型不是。類成員由它的域、方法、成員類和成員接口組成。方法的簽名由它的名字、正式的參數類型組成;簽名不包括方法的返回值類型。

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語言規范不同的術語。不像Java語言規范,本書使用繼承作為子類的同義詞。不再使用接口繼承的術語,本書簡單表述一個類實現了一個接口或一個接口擴展了另一個接口。為了描述沒有指定訪問級別的情況,本書使用描述術語包私有代替技術上正確的術語缺省訪問[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語言規范沒有定義的術語。術語exported APIsimply API,指的是類、接口、構造函數、成員、序列化形式,程序員通過它們訪問類、接口或包。(術語API,是應用程序接口的縮寫,優先使用API而不是其他人更喜歡的術語接口,是為了避免與Java語言中的接口相混淆。)程序員寫程序使用API指的是API的用戶。類中實現使用了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.

類、接口、構造函數、成員和序列化形式統稱為API元素。導出API由定義API的包的包外能訪問的API元素組成。這些API元素是任何客戶都能使用的并且API的作者提供支持。無獨有偶,Java工具類默認操作模式下也為這些元素產生了文檔。不嚴格的說,包的導出API由公有成員、保護成員和每個公有類的構造函數或包中的接口組成。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容