Effective Java - GBV

Transcription

Effective JavaSecond EditionJoshua BlochAAddison-WesleyUpper Saddle River, NJ Boston Indianapolis San FranciscoNew York Toronto Montreal London Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico City

ContentsForewordxiiiPrefacexvAcknowledgmentsxix1 Introduction12 Creating and Destroying Objects5Item 1: Consider static factory methods instead of constructors. 5Item 2: Consider a builder when faced with many constructorparameters11Item 3: Enforce the singleton property with a privateconstructor or an enum type17Item 4: Enforce noninstantiability with a private constructor. 19Item 5: Avoid creating unnecessary objects20Item 6: Eliminate obsolete object references24Item 7: Avoid finalizers273 Methods Common to All ObjectsItem 8: Obey the general contract when overriding equal sItem 9: Always override hashCode when youoverride equal sItem 10: Always override toStri ngItem 11: Override cl one judiciouslyItem 12: Consider implementing Comparable333345515462

CONTENTS4 Classes and InterfacesItem 13: Minimize the accessibility of classes and membersItem 14: In public classes, use accessor methods,not public fieldsItem 15: Minimize mutabilityItem 16: Favor composition over inheritanceItem 17: Design and document for inheritance or else prohibit itItem 18: Prefer interfaces to abstract classesItem 19: Use interfaces only to define typesItem 20: Prefer class hierarchies to tagged classesItem 21: Use function objects to represent strategiesItem 22: Favor static member classes over nonstatic5 GenericsItem 23: Don't use raw types in new codeItem 24: Eliminate unchecked warningsItem 25: Prefer lists to arraysItem 26: Favor generic typesItem 27: Favor generic methodsItem 28: Use bounded wildcards to increase API flexibilityItem 29: Consider typesafe heterogeneous containers6 Enums and AnnotationsItem 30: Use enums instead of i nt constantsItem 31: Use instance fields instead of ordinalsItem 32: Use EnumSet instead of bit fieldsItem 33: Use EnumMap instead of ordinal indexingItem 34: Emulate extensible enums with interfacesItem 35: Prefer annotations to naming patternsItem 36: Consistently use the Override annotationItem 37: Use marker interfaces to define types7 MethodsItem 38: Check parameters for validityItem 39: Make defensive copies when neededItem 40: Design method signatures carefullyItem 41: Use overloading judiciously6767717381. . 9161165169176179181181184189191

CONTENTSItem 42: Use varargs judiciously197Item 43: Return empty arrays or collections, not nulls201Item 44: Write doc comments for all exposed API elements . . . . 2038 General Programming209Item 45: Minimize the scope of local variables209Item 46: Prefer for-each loops to traditional for loops212Item 47: Know and use the libraries215Item 48: Avoid f 1 oat and doubl e if exact answersare required218Item 49: Prefer primitive types to boxed primitives221Item 50: Avoid strings where other types are more appropriate . . 224Item 51: Beware the performance of string concatenation227Item 52: Refer to objects by their interfaces228Item 53: Prefer interfaces to reflection230Item 54: Use native methods judiciously233Item 55: Optimize judiciously234Item 56: Adhere to generally accepted naming conventions2379 ExceptionsItem 57: Use exceptions only for exceptional conditionsItem 58: Use checked exceptions for recoverable conditionsand runtime exceptions for programming errorsItem 59: Avoid unnecessary use of checked exceptionsItem 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 indetail messagesItem 64: Strive for failure atomicityItem 65: Don't ignore exceptions10 ConcurrencyItem 66: Synchronize access to shared mutable dataItem 67: Avoid excessive synchronizationItem 68: Prefer executors and tasks to threadsItem 69: Prefer concurrency utilities to wai t and noti fу241241244246248250252254256258259259265271273XI

XllCONTENTSItem 70: Document thread safetyItem 71: Use lazy initialization judiciouslyItem 72: Don't depend on the thread schedulerItem 73: Avoid thread groups27828228628811 Serialization289Item 74: Implement Se realizable judiciouslyItem 75: Consider using a custom serialized formItem 76: Write readObject methods defensivelyItem 77: For instance control, prefer enum types289295302to readResol ve308Item 78: Consider serialization proxies instead of serializedinstances312Appendix: Items Corresponding to First Edition317References321Index327

Effective Java Second Edition Joshua Bloch AAddison-Wesley Upper Saddle River, NJ Boston Indianapolis San Francisco New York Toronto Montreal London