具体描述
Written for the working Java developer, Joshua Bloch's Effective Java Programming Language Guide provides a truly useful set of over 50 best practices and tips for writing better Java code. With plenty of advice from an indisputable expert in the field, this title is sure to be an indispensable resource for anyone who wants to get more out of their code.
As a veteran developer at Sun, the author shares his considerable insight into the design choices made over the years in Sun's own Java libraries (which the author acknowledges haven't always been perfect). Based on his experience working with Sun's best minds, the author provides a compilation of 57 tips for better Java code organized by category. Many of these ideas will let you write more robust classes that better cooperate with built-in Java APIs. Many of the tips make use of software patterns and demonstrate an up-to-the-minute sense of what works best in today's design. Each tip is clearly introduced and explained with code snippets used to demonstrate each programming principle.
Early sections on creating and destroying objects show you ways to make better use of resources, including how to avoid duplicate objects. Next comes an absolutely indispensable guide to implementing "required" methods for custom classes. This material will help you write new classes that cooperate with old ones (with advice on implementing essential requirements like the equals() and hashCode() methods).
The author has a lot to say about class design, whether using inheritance or composition. Tips on designing methods show you how to create understandable, maintainable, and robust classes that can be easily reused by others on your team. Sections on mapping C code (like structures, unions, and enumerated types) onto Java will help C programmers bring their existing skills to Sun's new language. Later sections delve into some general programming tips, like using exceptions effectively. The book closes with advice on using threads and synchronization techniques, plus some worthwhile advice on object serialization.
Whatever your level of Java knowledge, this title can make you a more effective programmer. Wisely written, yet never pompous or doctrinaire, the author has succeeded in packaging some really valuable nuggets of advice into a concise and very accessible guidebook that arguably deserves a place on most any developer's bookshelf. --Richard Dragan
Topics covered:
Best practices and tips for Java
Creating and destroying objects (static factory methods, singletons, avoiding duplicate objects and finalizers)
Required methods for custom classes (overriding equals(), hashCode(), toString(), clone(), and compareTo() properly)
Hints for class and interface design (minimizing class and member accessibility, immutability, composition versus inheritance, interfaces versus abstract classes, preventing subclassing, static versus nonstatic classes)
C constructs in Java (structures, unions, enumerated types, and function pointers in Java)
Tips for designing methods (parameter validation, defensive copies, method signatures, method overloading, zero-length arrays, hints for Javadoc comments)
General programming advice (local variable scope, using Java API libraries, avoiding float and double for exact comparisons, when to avoid strings, string concatenation, interfaces and reflection, avoid native methods, optimizing hints, naming conventions)
Programming with exceptions (checked versus run-time exceptions, standard exceptions, documenting exceptions, failure-capture information, failure atomicity)
Threading and multitasking (synchronization and scheduling hints, thread safety, avoiding thread groups)
Serialization (when to implement Serializable, the readObject(), and readResolve() methods)
作者简介
Joshua Bloch is chief Java architect at Google and a Jolt Award winner. He was previously a distinguished engineer at Sun Microsystems and a senior systems designer at Transarc. Bloch led the design and implementation of numerous Java platform features, including JDK 5.0 language enhancements and the award-winning Java Collections Framework. He coauthored Java™ Puzzlers (Addison-Wesley, 2005) and Java™ Concurrency in Practice (Addison-Wesley, 2006).
目录信息
Preface
Acknowledgments
1 Introduction
2 Creating and Destroying Objects
Item 1: Consider static factory methods instead of constructors
Item 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 references
Item 7: Avoid finalizers
3 Methods Common to All Objects
Item 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 judiciously
Item 12: Consider implementing Comparable
4 Classes and Interfaces
Item 13: Minimize the accessibility of classes and members
Item 14: In public classes, use accessor methods, not public fields
Item 15: Minimize mutability
Item 16: Favor composition over inheritance
Item 17: Design and document for inheritance or else prohibit it
Item 18: Prefer interfaces to abstract classes
Item 19: Use interfaces only to define types
Item 20: Prefer class hierarchies to tagged classes
Item 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 warnings
Item 25: Prefer lists to arrays
Item 26: Favor generic types
Item 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 constants
Item 31: Use instance fields instead of ordinals
Item 32: Use EnumSet instead of bit fields
Item 33: Use EnumMap instead of ordinal indexing
Item 34: Emulate extensible enums with interfaces
Item 35: Prefer annotations to naming patterns
Item 36: Consistently use the Override annotation
Item 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 carefully
Item 41: Use overloading judiciously
Item 42: Use varargs judiciously
Item 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 variables
Item 46: Prefer for-each loops to traditional for loops
Item 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 judiciously
Item 55: Optimize judiciously
Item 56: Adhere to generally accepted naming conventions
9 Exceptions
Item 57: Use exceptions only for exceptional conditions
Item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
Item 59: Avoid unnecessary use of checked exceptions
Item 60: Favor the use of standard exceptions
Item 61: Throw exceptions appropriate to the abstraction
Item 62: Document all exceptions thrown by each method
Item 63: Include failure-capture information in detail messages
Item 64: Strive for failure atomicity
Item 65: Don't ignore exceptions
10 Concurrency
Item 66: Synchronize access to shared mutable data
Item 67: Avoid excessive synchronization
Item 68: Prefer executors and tasks to threads
Item 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 groups
11 Serialization
Item 74: Implement Serializable judiciously
Item 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 serialized instances
Appendix: Items Corresponding to First Edition
References
Index
· · · · · · (收起)
读后感
书是好书,但是翻译简直不堪卒读,有些地方我估计译者可能自己现在都看不懂。英语水平可以的话还是建议和英文版对比着读,我对比的时候就发现几个容易让读者迷惑的地方。比如在第 37 条的最后一段: 原文是: “In a sense, this item is the inverse of Item 19, which says,...
内容讲解得很到位。。。。。。。。。。。。。。。。。。。。 内容讲解得很到位。。。。。。。。。。。。。。。。。。。。 内容讲解得很到位。。。。。。。。。。。。。。。。。。。。 内容讲解得很到位。。。。。。。。。。。。。。。。。。。。 内容讲解得很到位。。。。。。...
很早就读过,当时就知道这本书很好,可惜当时功力尚浅,没什么收获。但近日再读时,确实很有收获,可以说此书虽不是深入骨髓,但也算入木三分。新手勿动!
如果你使用刚刚学会的Java做了一个小应用程序,那么你就可以开始有选择地看这本书。书中分别对Java的不同特性分章节给予作者本人的建议。如果你还没有用到其中的某一特性,那么就没必要读相关的章节,跳过去。只有你经历过了,摔倒过了,困扰过了,你才会与书中的建议产生共鸣...
用户评价
这是一本需要反复揣摩的书。我承认,初读的时候,有些章节我可能只是“似懂非懂”,感觉作者在讲一些非常底层、非常精妙的东西,需要静下心来,结合自己的实际开发经验去消化。但正是这种“难啃”的特质,反而让我觉得它价值非凡。这本书没有给任何“万能药”,而是教会你“思考”的方法,让你能够独立地去分析和解决问题。 尤其是在处理并发和性能优化方面,作者的见解非常独到。我记得有一个关于“如何正确使用同步机制”的章节,读完之后,我才意识到自己之前对`synchronized`关键字的理解有多么片面,以及它在多线程环境下的潜在性能影响。作者不仅指出了问题所在,还给出了更优的替代方案,并且详细解释了背后的原理。这种深入骨髓的讲解,让我对Java的理解上升了一个层次,不再是停留在表面的语法层面,而是能够真正掌握其内在的运行机制。
这本《Effective Java: Second Edition》真的像一本武林秘籍,不过不是教你如何降龙十八掌,而是让你在Java的江湖里如鱼得水,少走弯路。我拿到这本书的时候,说实话,对Java已经有了一定的了解,觉得自己还算是个“熟练工”。但是读了这本书,才发现自己之前的一些“好习惯”,可能在更高级别的Java开发者看来,简直是“画蛇添足”或者“南辕北辙”。作者( Joshua Bloch)真是个老江湖,他能用最精炼的语言,剖析Java语言设计中的一些微妙之处,以及在实际开发中如何避免那些看似微不足道却可能导致巨大问题的陷阱。 我尤其喜欢他关于“条理清晰的设计”的那几个章节,以前我写代码,总觉得功能实现就好,至于代码是否优雅、易于维护,总是被放到次要位置。这本书让我意识到,清晰的代码结构和设计模式的应用,远比想象中重要得多。它不仅能让你自己以后修改代码时省去大量时间,更能让你的团队成员更容易理解你的意图,大大降低了沟通成本和协作难度。而且,他关于“避免过度设计”的论述也相当到位,告诫我们不要为了使用某种模式而强行套用,而是要根据实际需求来选择最合适的解决方案。这种平衡的观点,在很多技术书中都很难得。
《Effective Java: Second Edition》这本书,对我来说,更像是一本“内功心法”。它没有直接教你“招式”,而是让你去理解“力道”和“节奏”。我以前写Java代码,总感觉是在“堆砌”代码,追求的是功能的实现。但这本书让我看到了代码的“生命力”,以及如何让它变得更“健壮”、“高效”和“优雅”。 我印象特别深刻的是关于“如何使用枚举来表示常量”的讲解,之前我一直是用`final static`来定义常量,觉得挺方便的。但是读了这本书,才明白枚举在处理常量集合、保证类型安全以及在某些复杂场景下的优势。作者不仅给出了“为什么”,还给出了“怎么做”,并且解释了背后的设计理念。这种“追根溯源”的学习方式,让我对Java语言的认识更加深刻,也让我自己在写代码时,多了一份审慎和考量。它让我明白,好的代码不仅仅是能运行,更是经得起时间的考验,易于维护和扩展。
读完《Effective Java: Second Edition》,我最大的感受就是,原来Java开发可以这么“讲究”。这本书不是那种让你看了之后立马就能写出“高大上”代码的书,它更多的是在培养你的“Java编程思维”。作者 Joshua Bloch用他深厚的功力,把Java语言的设计哲学和最佳实践,以一种近乎“艺术”的方式呈现出来。 书中关于“优先考虑使用接口而不是实现类”的解释,让我深刻理解了“依赖倒置原则”在实际应用中的重要性。我之前也知道这个原则,但总是觉得有点抽象,直到看了这本书,才明白它对于代码的可扩展性、可测试性有多么大的影响。作者通过生动的例子,把抽象的概念变得具体可感,让我不再是“纸上谈兵”。而且,这本书的结构非常清晰,每个“条目”都聚焦于一个具体的问题,并且给出了清晰的解决方案和原因分析,非常适合碎片化阅读,也便于随时查阅。
老实说,我拿起《Effective Java: Second Edition》这本书的时候,内心是抱着一种“学习前人经验,避免自己踩坑”的心态。毕竟,Java这门语言已经发展了很多年,积累了无数的实践经验和教训。而这本书,简直就是这些宝贵经验的精华提炼。它不是那种泛泛而谈、告诉你“应该做什么”的书,而是深入到Java语言的每一个细节,告诉你“为什么这么做”以及“这样做有什么好处”。 举个例子,书中关于“使用对象而不是引用来比较”的讲解,我之前在项目中也遇到过类似的问题,但总是一知半解,有时候能碰巧解决,有时候就莫名其妙地出现bug。读了这本书,我才恍然大悟,原来背后的原因如此清晰,并且作者给出了最有效的避免方法。这种“醍醐灌顶”的感觉,贯穿了整本书。它就像一个经验丰富的老司机,在指导你如何驾驶Java这辆车,让你不仅能开得快,更能开得稳,而且不容易发生意外。
不错的java进阶读物~~~
Java在进步!
后面两百页开始用浏览的方式阅读,并且略过了枚举和声明式的部分以及部分泛型的内容。
大部分都是精华,废话很少
咦我之前居然没把这本记到豆瓣…