推荐序前言1 Introduction2 Creating and Destroying ObjectsItem 1: Consider static factory methods instead of constructorsItem 2: Consider a builder when faced with many constructor parameters Item 3: Enforce the singleton property with a private constructor or an enum type Item 4: Enforce noninstantiability with a private constructor Item 5: Avoid creating unnecessary objects Item 6: Eliminate obsolete object referencesItem 7: Avoid finalizers3 Methods Common to All ObjectsItem 8: Obey the general contract when overriding equals Item 9: Always override hashCode when you override equals Item 10: Always override toString Item 11: Override clone judiciouslyItem 12: Consider implementing Comparable 4 Classes and InterfacesItem 13: Minimize the accessibility of classes and membersItem 14: In public classes, use accessor methods, not public fields Item 15: Minimize mutabilityItem 16: Favor composition over inheritanceItem 17: Design and document for inheritance or else prohibit it Item 18: Prefer interfaces to abstract classes Item 19: Use interfaces only to define typesItem 20: Prefer class hierarchies to tagged classesItem 21: Use function objects to represent strategies Item 22: Favor static member classes over nonstatic 5 Generics Item 23: Don’t use raw types in new code Item 24: Eliminate unchecked warningsItem 25: Prefer lists to arrays Item 26: Favor generic typesItem 27: Favor generic methods Item 28: Use bounded wildcards to increase API flexibility Item 29: Consider typesafe heterogeneous containers 6 Enums and Annotations Item 30: Use enums instead of int constantsItem 31: Use instance fields instead of ordinals Item 32: Use EnumSet instead of bit fieldsItem 33: Use EnumMap instead of ordinal indexingItem 34: Emulate extensible enums with interfaces Item 35: Prefer annotations to naming patterns Item 36: Consistently use the Override annotationItem 37: Use marker interfaces to define types 7 Methods Item 38: Check parameters for validity Item 39: Make defensive copies when needed Item 40: Design method signatures carefullyItem 41: Use overloading judiciouslyItem 42: Use varargs judiciouslyItem 43: Return empty arrays or collections, not nulls Item 44: Write doc comments for all exposed API elements 8 General Programming Item 45: Minimize the scope of local variablesItem 46: Prefer for-each loops to traditional for loopsItem 47: Know and use the libraries Item 48: Avoid float and double if exact answers are required Item 49: Prefer primitive types to boxed primitives Item 50: Avoid strings where other types are more appropriate Item 51: Beware the performance of string concatenation Item 52: Refer to objects by their interfaces Item 53: Prefer interfaces to reflection Item 54: Use native methods judiciouslyItem 55: Optimize judiciously Item 56: Adhere to generally accepted naming conventions9 Exceptions Item 57: Use exceptions only for exceptional conditions Item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errorsItem 59: Avoid unnecessary use of checked exceptions Item 60: Favor the use of standard exceptionsItem 61: Throw exceptions appropriate to the abstractionItem 62: Document all exceptions thrown by each methodItem 63: Include failure-capture information in detail messages Item 64: Strive for failure atomicity Item 65: Don’t ignore exceptions 10 ConcurrencyItem 66: Synchronize access to shared mutable dataItem 67: Avoid excessive synchronization Item 68: Prefer executors and tasks to threadsItem 69: Prefer concurrency utilities to wait and notify Item 70: Document thread safety Item 71: Use lazy initialization judiciously Item 72: Don’t depend on the thread scheduler Item 73: Avoid thread groups11 SerializationItem 74: Implement Serializable judiciouslyItem 75: Consider using a custom serialized form Item 76: Write readObject methods defensively Item 77: For instance control, prefer enum types to readResolve Item 78: Consider serialization proxies instead of seri
內容試閱:
推荐序如果有同事对你说,“Spouse of me this night today manufactures the unusual meal in a home. You will join?”这时候你脑子里可能会浮现出三件事情:同事在邀请你参加他的家庭晚宴,英语肯定不是你这位同事的母语,更多的则是满脑子的疑惑。如果你曾经学习过第二种语言,并且尝试过在课堂之外使用这种语言,你就该知道有三件事情是必须掌握的:这门语言的结构如何(语法)、如何命名你想谈论的事物(词汇),以及如何以惯用和高效的方式来表达日常的事物(用法)。在课堂上大多只涉及到前面两点,当你使出浑身解数想让对方明白你的意思时,常常会发现当地人对你的表述忍俊不禁。程序设计语言也是如此。你需要理解语言的核心:它是面向算法的,还是面向函数的,或者是面向对象的?你需要知道词汇表:标准类库提供了哪些数据结构、操作和功能设施(Facility)?你还需要熟悉如何用习惯和高效的方式来构建代码。也许是因为前面两点比较容易编写,所以关于程序设计语言的书籍通常只涉及这两点,或者只是蜻蜓点水般地介绍一下用法。语法和词汇是语言本身固有的特性,但是用法则反映了使用这门语言的群体特征。例如,Java程序设计语言是一门支持单继承的面向对象程序设计语言,在每个方法的内部,它都支持命令式的(面向语句的,Statement-Oriented)编码风格。Java类库提供了对图形显示、网络、分布式计算和安全性的支持。但是,如何把这门语言以最佳的方式运用到实践中呢?还有一点,程序与口头的句子以及大多数书籍和杂志都不同,它会随着时间的推移而发生变化。仅仅编写出能够有效地工作并且能够被别人理解的代码往往是不够的,我们还必须把代码组织成易于修改的形式。针对某个任务可能会有10种不同的编码方法,而在这10种编码方法中,有7种编码方法是笨拙的、低效的或者是难以理解的。而在剩下的3种编码方法中,哪一种会是最接近该任务的下一年度发行版本的代码呢?目前有大量的书籍可以供你学习Java程序设计语言的语法,包括The Java Programming Language [Arnold05](作者Arnold、Gosling和Holmes),以及The Java Language Specification [JLS](作者Gosling、Joy和Bracha)。同样地,与Java程序设计语言相关的类库和API的书籍也有很多。本书解决了你的第三种需求,即如何以惯用和高效的方式来表达日常的事物(用法)。多年来,作者Joshua Bloch在Sun Microsystems公司一直从事Java语言的扩展、实现和使用的工作;他还大量地阅读了其他人的代码,包括我的代码。他在本书中提出了许多很好的建议,他系统地把这些建议组织起来,旨在告诉读者如何更好地构造代码以便它们能工作得更好,也便于其他人能够理解这些代码,而且将来对代码进行修改和改善时也不至于那么头疼。甚至,你的程序也会因此而变得更加令人愉悦、更加优美和雅致。Guy L. Steele JrBurlington, Massachusetts2001年4月