你正在寻找一本简明扼要地阐述Java精髓的书吗?你希望深入地理解Java程序设计语言吗?你希望编写出清晰、正确、健壮和可复用的代码吗?不用再找了!你手上的这本书就能在实现你这些愿望的同时,为你提供许多意想不到的好处。
本书介绍了Java编程中的57条极具实用价值的经验规则,这些规则涵盖了大多数开发人员每天所要面临问题的解决方案。通过对Java编程平台设计专家所使用技术的全面描述,提示了在生成清晰、健壮和高效的代码过程中,应该做什么和不应该做什么。
书中的每条规则都以简短、独立的形式出现,包括了详细而精确的建议,以及对Java语言中许多细微之处的深入分析,并通过例子代码进一步加以说明。贯穿全书的是通用 的语言用法和设计模式,以及一些具有启发意义的技巧和技术。
主要内容包括:
● 通行和高效的语言用法,以简明、可读和易于使用的形式介绍专家的建议
● 有助于你最有效地使用Java平台的模式、反模式及习惯用法
● Java语言及其库中通常被误解的细微之处:如何避免这些陷阱和缺陷
● 关注Jaava语言本身及其最基本的库:java.lang、java.util和一个较小的扩展java.io
● 关于序列化的详细介绍,其中包括其他地方没有提及的一些实践建议
Foreword
Preface
Acknowledgments
1 Introduction
2 Creating and Destroying Objects
Item 1: Consider providing static factory methods instead of constructors
Item 2: Enforce the singleton property with a private consructor
Item 3: Enforce noninstantiability with a private consstructor
Item 4: Avoid creating duplicate objects
Item 5: Eliminate obsolete object references
Item 6: Avoid finalizers
3 Methods Common to All Objects
Item 7: Obey the general contract when overriding equals
Item 8: Always override hashCode when you override equals
Item 9: Always override toString
Item 10: Override clone judiciously
Item 11: consider implementing Comparable
4 Classes and Interfaces
Item 12: minimize the accessibility of classes and members
Item 13: Favor immutablity
Item 14: Favor composition over inheritance
Item 15: Design and document for inheritance or else prohibit it
Item 16: Prefer interfaces to abstract classes
Item 17: Use interfaces only to define types
Item 18: Favor static member classes over nonstatic
5 Substitutes for C Constructs
Item 19: Replace structures with lasses
Item 20: Replace unions with class hierarchies
Item 21: Replace enum constructs with classes
Item 22: Replace function pointers with classes and interfaces
6 Methods
Item 23: check parameters for validity
Item 24: Make defensive copies when needed
Item 25: Design method signatures carefully
Item 26: Use overloading judiciously
Item 27: Return zero-length arrays, not nulls
Item 28: Write doc comments for all exposed API elements
7 General Programming
Item 29: Minimize the scope of local variables
Item 30: Know and use the libraries
Item 31: Avoid float and double if exact asnwers are required
Item 32: Avoid strings where other types are more appropriate
Item 33: Beware the performance of string concatenation
Item 34: Refer to objects by their interfaces
Item 35: Prefer interfaces to reflection
Item 36: Use native methods judiciously
Item 37: Optimize judiciously
Item 38: Abhere to generally accepted naming conventions
8 Exceptions
Item 39: Use exceptions only for exceptional conditions
Item 40: Use checked exceptions for recoverable conditions and run-time exceptions for programming errors
Item 41: Avoid unnecessary use of checked excceptions
Item 42: Favor the use of standard exceptions
Item 43: Throw exceptions appropriate to the abstraction
Item 44: Document all exceptions thrown by each method
Item 45: Include failure-capture information in detail messages
Item 46: Strive for failure atomicity
Item 47: Don't ignore exceptions
9 Threads
Item 48: Synchronize access to shared mutable data
Item 49: Avoide excessive synchronization
Item 50: never invoke wait outside a loop
Item 51: Don't depend on the thread scheduler
Item 52: Document thread safety
Item 53: Avoid thread groups
10 Serialization
Item 54: Implement Serializable judiciously
Item 55: Consider using a custom serialized form
Item 56: Write readObject methods defensively
Item 57: Provide a readResolve method when necessary
References
Index of Patterns and Idioms
Index
In 1996 I pulled up stakes and headed west to work for JavaSoft, as it was then known, because it was clear that that was where the action was. In the intervening five years I've served as Java platform libraries architect. I've designed, implemented, and maintained many of the libraries and served as a consultant for many others. Presiding over these libraries as the Java Platform matured was a once-in-a-lifetime opportunity. It is no exaggeration to say that I had the privilege to work with some of the great software engineers of our generation. In the process, I learned a lot about the Java programming language--what works, what doesn't,and how to use the language and its libraries to best effect.
This book is my attempt to share my experience with you so that you can imitate my successes while avoiding my failures. I borrowed the format from Scott Meyers's Effective C++ [Meyer98], which consists of fifty items, each conveying one specific rule for improving your programs and designs. I found the format to be singularly effective, and I hope you do too.
In many cases, I took the liberty of illustrating the items with real-world examples from the Java platform libraries. When describing something that could have been done better, I tried to pick on code that I wrote myself, but occasionally I pick on something written by a colleague. I sincerely apologize if, despite my best efforts, I've offended anyone. Negative examples are cited not to cast blame but in the spirit of cooperation, so that all of us can benefit from the experience of those who've gone before.
While this book is not targeted solely at developers of reusabel components, it is inevitably colored by my experience writing such components over the past two decades. I narurally think in terms of exported APIs (Application Programming Interfaces),and I encourage you to do likewise. Even if you aren't developing reusable component, thinking in these terms tends to improve the quality of the software you write. Furthermore, it's not uncommon to write a reusable component without knowing it: You write something useful, share it with your buddy across the hall, and before long you have half a dozen users. At this point, you no longer have the flexibility to change the API at will nad are thankful for all the effort that you put into designing the API when you fist wrote the softwar.
My focus on API design may seem a bit unnatural to devotees of the new lightweight software development methodlogies, such as Extreme Programming [Beck99]. These methodologies emphasize writing the simplest program that could possibly work. If you're using one of these methodologies, you'll find that a focus on API design serves you well in the refactoring process. The fundamental goals of refactoring are the improvement of system structure and the avoidance of code duplication. These goals are impossible to achieve in the absence of well-designed APIs for the components of the system.
No language is perfect, but some are excellent. I have found the Java programming language and its libraries to be immensely conducive to quality and producitivity, and a joy to work with. I hope this book captures my enthusiasm and helps make your use of the language more effective and enjoyable.