0%

Book Description

The Definitive Guide to Java Platform Best Practices–Updated for Java 7, 8, and 9

Java has changed dramatically since the previous edition of Effective Java was published shortly after the release of Java 6. This Jolt award-winning classic has now been thoroughly updated to take full advantage of the latest language and library features. The support in modern Java for multiple paradigms increases the need for specific best-practices advice, and this book delivers.

As in previous editions, each chapter of Effective Java, Third Edition, consists of several “items,” each presented in the form of a short, stand-alone essay that provides specific advice, insight into Java platform subtleties, and updated code examples. The comprehensive descriptions and explanations for each item illuminate what to do, what not to do, and why.

The third edition covers language and library features added in Java 7, 8, and 9, including the functional programming constructs that were added to its object-oriented roots. Many new items have been added, including a chapter devoted to lambdas and streams.

New coverage includes

  • Functional interfaces, lambda expressions, method references, and streams

  • Default and static methods in interfaces

  • Type inference, including the diamond operator for generic types

  • The @SafeVarargs annotation

  • The try-with-resources statement

  • New library features such as the Optional interface, java.time, and the convenience factory methods for collections

Table of Contents

  1. Cover Page
  2. About This E-Book
  3. Title Page
  4. Copyright Page
  5. Dedication
  6. Contents
  7. Foreword
  8. Preface
  9. Acknowledgments
  10. 1 Introduction
  11. 2 Creating and Destroying Objects
    1. Item 1: Consider static factory methods instead of constructors
    2. Item 2: Consider a builder when faced with many constructor parameters
    3. Item 3: Enforce the singleton property with a private constructor or an enum type
    4. Item 4: Enforce noninstantiability with a private constructor
    5. Item 5: Prefer dependency injection to hardwiring resources
    6. Item 6: Avoid creating unnecessary objects
    7. Item 7: Eliminate obsolete object references
    8. Item 8: Avoid finalizers and cleaners
    9. Item 9: Prefer try-with-resources to try-finally
  12. 3 Methods Common to All Objects
    1. Item 10: Obey the general contract when overriding equals
    2. Item 11: Always override hashCode when you override equals
    3. Item 12: Always override toString
    4. Item 13: Override clone judiciously
    5. Item 14: Consider implementing Comparable
  13. 4 Classes and Interfaces
    1. Item 15: Minimize the accessibility of classes and members
    2. Item 16: In public classes, use accessor methods, not public fields
    3. Item 17: Minimize mutability
    4. Item 18: Favor composition over inheritance
    5. Item 19: Design and document for inheritance or else prohibit it
    6. Item 20: Prefer interfaces to abstract classes
    7. Item 21: Design interfaces for posterity
    8. Item 22: Use interfaces only to define types
    9. Item 23: Prefer class hierarchies to tagged classes
    10. Item 24: Favor static member classes over nonstatic
    11. Item 25: Limit source files to a single top-level class
  14. 5 Generics
    1. Item 26: Don’t use raw types
    2. Item 27: Eliminate unchecked warnings
    3. Item 28: Prefer lists to arrays
    4. Item 29: Favor generic types
    5. Item 30: Favor generic methods
    6. Item 31: Use bounded wildcards to increase API flexibility
    7. Item 32: Combine generics and varargs judiciously
    8. Item 33: Consider typesafe heterogeneous containers
  15. 6 Enums and Annotations
    1. Item 34: Use enums instead of int constants
    2. Item 35: Use instance fields instead of ordinals
    3. Item 36: Use EnumSet instead of bit fields
    4. Item 37: Use EnumMap instead of ordinal indexing
    5. Item 38: Emulate extensible enums with interfaces
    6. Item 39: Prefer annotations to naming patterns
    7. Item 40: Consistently use the Override annotation
    8. Item 41: Use marker interfaces to define types
  16. 7 Lambdas and Streams
    1. Item 42: Prefer lambdas to anonymous classes
    2. Item 43: Prefer method references to lambdas
    3. Item 44: Favor the use of standard functional interfaces
    4. Item 45: Use streams judiciously
    5. Item 46: Prefer side-effect-free functions in streams
    6. Item 47: Prefer Collection to Stream as a return type
    7. Item 48: Use caution when making streams parallel
  17. 8 Methods
    1. Item 49: Check parameters for validity
    2. Item 50: Make defensive copies when needed
    3. Item 51: Design method signatures carefully
    4. Item 52: Use overloading judiciously
    5. Item 53: Use varargs judiciously
    6. Item 54: Return empty collections or arrays, not nulls
    7. Item 55: Return optionals judiciously
    8. Item 56: Write doc comments for all exposed API elements
  18. 9 General Programming
    1. Item 57: Minimize the scope of local variables
    2. Item 58: Prefer for-each loops to traditional for loops
    3. Item 59: Know and use the libraries
    4. Item 60: Avoid float and double if exact answers are required
    5. Item 61: Prefer primitive types to boxed primitives
    6. Item 62: Avoid strings where other types are more appropriate
    7. Item 63: Beware the performance of string concatenation
    8. Item 64: Refer to objects by their interfaces
    9. Item 65: Prefer interfaces to reflection
    10. Item 66: Use native methods judiciously
    11. Item 67: Optimize judiciously
    12. Item 68: Adhere to generally accepted naming conventions
  19. 10 Exceptions
    1. Item 69: Use exceptions only for exceptional conditions
    2. Item 70: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
    3. Item 71: Avoid unnecessary use of checked exceptions
    4. Item 72: Favor the use of standard exceptions
    5. Item 73: Throw exceptions appropriate to the abstraction
    6. Item 74: Document all exceptions thrown by each method
    7. Item 75: Include failure-capture information in detail messages
    8. Item 76: Strive for failure atomicity
    9. Item 77: Don’t ignore exceptions
  20. 11 Concurrency
    1. Item 78: Synchronize access to shared mutable data
    2. Item 79: Avoid excessive synchronization
    3. Item 80: Prefer executors, tasks, and streams to threads
    4. Item 81: Prefer concurrency utilities to wait and notify
    5. Item 82: Document thread safety
    6. Item 83: Use lazy initialization judiciously
    7. Item 84: Don’t depend on the thread scheduler
  21. 12 Serialization
    1. Item 85: Prefer alternatives to Java serialization
    2. Item 86: Implement Serializable with great caution
    3. Item 87: Consider using a custom serialized form
    4. Item 88: Write readObject methods defensively
    5. Item 89: For instance control, prefer enum types to readResolve
    6. Item 90: Consider serialization proxies instead of serialized instances
  22. Items Corresponding to Second Edition
  23. References
  24. Index
  25. Code Snippets