內容簡介
自從Java 6發布之後,Java又有了翻天覆地的變化。本書涵蓋了Java 7、Java 8和Java 9中語言和庫的各種新特性。讓你能夠深入了解Java平台的細微之處。通過對每一個項目的全面描述和解釋,告訴你應該做什麼、不應該做什麼,以及為什麼要這樣做。
圖書目錄
1Introduction1
2CreatingandDestroyingObjects5
Item1:Considerstaticfactorymethodsinsteadofconstructors5
Item2:Considerabuilderwhenfacedwithmanyconstructorparameters10
Item3:Enforcethesingletonpropertywithaprivateconstructororanenumtype17
Item4:Enforcenoninstantiabilitywithaprivateconstructor19
Item5:Preferdependencyinjectiontohardwiringresources20
Item6:Avoidcreatingunnecessaryobjects22
Item7:Eliminateobsoleteobjectreferences26
Item8:Avoidfinalizersandcleaners29
Item9:Prefertry-with-resourcestotry-finally.34
3MethodsCommontoAllObjects37
Item10:Obeythegeneralcontractwhenoverridingequals37
Item11:AlwaysoverridehashCodewhenyouoverrideequals50
Item12:AlwaysoverridetoString55
Item13:Overrideclonejudiciously58
Item14:ConsiderimplementingComparable66
4ClassesandInterfaces73
Item15:Minimizetheaccessibilityofclassesandmembers73
Item16:Inpublicclasses,useaccessormethods,notpublicfields78
Item17:Minimizemutability80
Item18:Favorcompositionoverinheritance87
Item19:Designanddocumentforinheritanceorelseprohibitit93
Item20:Preferinterfacestoabstractclasses99
Item21:Designinterfacesforposterity104
Item22:Useinterfacesonlytodefinetypes.107
Item23:Preferclasshierarchiestotaggedclasses109
Item24:Favorstaticmemberclassesovernonstatic112
Item25:Limitsourcefilestoasingletop-levelclass115
5Generics.117
Item26:Don’tuserawtypes117
Item27:Eliminateuncheckedwarnings.123
Item28:Preferliststoarrays126
Item29:Favorgenerictypes.130
Item30:Favorgenericmethods135
Item31:UseboundedwildcardstoincreaseAPIflexibility139
Item32:Combinegenericsandvarargsjudiciously.146
Item33:Considertypesafeheterogeneouscontainers151
6EnumsandAnnotations157
Item34:Useenumsinsteadofintconstants.157
Item35:Useinstancefieldsinsteadofordinals168
Item36:UseEnumSetinsteadofbitfields169
Item37:UseEnumMapinsteadofordinalindexing.171
Item38:Emulateextensibleenumswithinterfaces176
Item39:Preferannotationstonamingpatterns180
Item40:ConsistentlyusetheOverrideannotation.188
Item41:Usemarkerinterfacestodefinetypes191
7LambdasandStreams193
Item42:Preferlambdastoanonymousclasses193
Item43:Prefermethodreferencestolambdas197
Item44:Favortheuseofstandardfunctionalinterfaces199
Item45:Usestreamsjudiciously203
Item46:Preferside-effect-freefunctionsinstreams210
Item47:PreferCollectiontoStreamasareturntype.216
Item48:Usecautionwhenmakingstreamsparallel222
8Methods227
Item49:Checkparametersforvalidity227
Item50:Makedefensivecopieswhenneeded231
Item51:Designmethodsignaturescarefully236
Item52:Useoverloadingjudiciously238
Item53:Usevarargsjudiciously245
Item54:Returnemptycollectionsorarrays,notnulls247
Item55:Returnoptionalsjudiciously249
Item56:WritedoccommentsforallexposedAPIelements254
9GeneralProgramming261
Item57:Minimizethescopeoflocalvariables261
Item58:Preferfor-eachloopstotraditionalforloops264
Item59:Knowandusethelibraries267
Item60:Avoidfloatanddoubleifexactanswersarerequired270
Item61:Preferprimitivetypestoboxedprimitives273
Item62:Avoidstringswhereothertypesaremoreappropriate276
Item63:Bewaretheperformanceofstringconcatenation279
Item64:Refertoobjectsbytheirinterfaces280
Item65:Preferinterfacestoreflection282
Item66:Usenativemethodsjudiciously.285
Item67:Optimizejudiciously286
Item68:Adheretogenerallyacceptednamingconventions289
10Exceptions293
Item69:Useexceptionsonlyforexceptionalconditions293
Item70:Usecheckedexceptionsforrecoverableconditionsandruntimeexceptionsforprogrammingerrors296
Item71:Avoidunnecessaryuseofcheckedexceptions298
Item72:Favortheuseofstandardexceptions.300
Item73:Throwexceptionsappropriatetotheabstraction.302
Item74:Documentallexceptionsthrownbyeachmethod.304
Item75:Includefailure-captureinformationindetailmessages.306
Item76:Striveforfailureatomicity308
Item77:Don’tignoreexceptions310
11Concurrency311
Item78:Synchronizeaccesstosharedmutabledata311
Item79:Avoidexcessivesynchronization317
Item80:Preferexecutors,tasks,andstreamstothreads323
Item81:Preferconcurrencyutilitiestowaitandnotify325
Item82:Documentthreadsafety330
Item83:Uselazyinitializationjudiciously333
Item84:Don’tdependonthethreadscheduler336
12Serialization339
Item85:PreferalternativestoJavaserialization339
Item86:ImplementSerializablewithgreatcaution343
Item87:Considerusingacustomserializedform346
Item88:WritereadObjectmethodsdefensively353
Item89:Forinstancecontrol,preferenumtypestoreadResolve359
Item90:Considerserializationproxiesinsteadofserializedinstances363
ItemsCorrespondingtoSecondEdition367
References.371
Index377
作者簡介
Joshua Bloch是Java 集合框架創辦人,領導了很多 Java 平台特性的設計和實現,包括 JDK 5.0 語言增強以及屢獲殊榮的 Java 集合框架。2004年6月他離開了SUN公司並成為 Google 的首席 Java 架構師。此外他還因為《Effective Java》一書獲得著名的 Jolt 大獎。