文章作者: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.lang
,java.util
,在更小程度上包括java.util.concurrent
和java.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平台增加了许多新功能。本书中的大多数条目在某种程度上使用了这些功能。下表列出了这些新功能在本书中的位置:
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
值得注意的是,我的机器是老旧的组装电脑,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编程语言的特性和它的库时,有时指明特定的版本是必要的。为了简洁,本书使用工程版本号而不是正式的发行名称。下表显示了发行名称与工程版本号的映射关系。
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.util
和java.io
中的类。为了编译这些例子,你可能必须添加一个或多个导入声明:
1 | import java.util.*; |
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 API
或simply 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由公有成员、保护成员和每个公有类的构造函数或包中的接口组成。