Effective Java (2nd ed.) [Bloch 2008-05-28].pdf
(
1070 KB
)
Pobierz
Effective Java
™
Second Edition
Joshua Bloch
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where
those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been
printed with initial capital letters or in all capitals.
Sun Microsystems, Inc. has intellectual property rights relating to implementations of the technology described in this pub-
lication. In particular, and without limitation, these intellectual property rights may include one or more U.S. patents, for-
eign patents, or pending applications.
Sun, Sun Microsystems, the Sun logo, J2ME, J2EE, Java Card, and all Sun and Java based trademarks and logos are trade-
marks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries. UNIX is a registered
trademark in the United States and other countries, exclusively licensed through X/Open Company, Ltd. THIS PUBLICA-
TION IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUD-
ING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THIS PUBLICATION COULD INCLUDE TECHNICAL
INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMA-
TION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN
MICROSYSTEMS, INC. MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR THE
PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any
kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages
in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which
may include electronic versions and/or custom covers and content particular to your business, training goals, marketing
focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside the United States please contact:
International Sales
international@pearsoned.com
Visit us on the Web: informit.com/aw
Library of Congress Control Number:
2008926278
Copyright © 2008 Sun Microsystems, Inc.
4150 Network Circle,
Santa Clara, California 95054 U.S.A.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permis-
sion must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or trans-
mission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information
regarding permissions, write to:
Pearson Education, Inc.
Rights and Contracts Department
501 Boylston Street, Suite 900
Boston, MA 02116
Fax: (617) 671-3447
ISBN-13: 978-0-321-35668-0
ISBN-10:
0-321-35668-3
Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts.
First printing, May 2008
Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
2 Creating and Destroying Objects. . . . . . . . . . . . . . . . . . .5
Item 1: Consider static factory methods instead of constructors . . . 5
Item 2: Consider a builder when faced with many constructor
parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Item 3: Enforce the singleton property with a private
constructor or an enum type . . . . . . . . . . . . . . . . . . . . . . . 17
Item 4: Enforce noninstantiability with a private constructor . . . . 19
Item 5: Avoid creating unnecessary objects . . . . . . . . . . . . . . . . . 20
Item 6: Eliminate obsolete object references . . . . . . . . . . . . . . . . . 24
Item 7: Avoid finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3 Methods Common to All Objects. . . . . . . . . . . . . . . . . .33
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
. . . . . . . . . . . . . . . .
33
45
51
54
62
ix
x
CONTENTS
4 Classes and Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Item 13: Minimize the accessibility of classes and members . . . . . . 67
Item 14: In public classes, use accessor methods,
not public fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Item 15: Minimize mutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Item 16: Favor composition over inheritance . . . . . . . . . . . . . . . . . . 81
Item 17: Design and document for inheritance or else prohibit it . . 87
Item 18: Prefer interfaces to abstract classes . . . . . . . . . . . . . . . . . . 93
Item 19: Use interfaces only to define types. . . . . . . . . . . . . . . . . . . 98
Item 20: Prefer class hierarchies to tagged classes . . . . . . . . . . . . . 100
Item 21: Use function objects to represent strategies . . . . . . . . . . . 103
Item 22: Favor static member classes over nonstatic . . . . . . . . . . . 106
5 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Item 23: Don’t use raw types in new code . . . . . . . . . . . . . . . . . . . 109
Item 24: Eliminate unchecked warnings. . . . . . . . . . . . . . . . . . . . . 116
Item 25: Prefer lists to arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Item 26: Favor generic types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Item 27: Favor generic methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Item 28: Use bounded wildcards to increase API flexibility . . . . . 134
Item 29: Consider typesafe heterogeneous containers . . . . . . . . . . 142
6 Enums and Annotations . . . . . . . . . . . . . . . . . . . . . . . . 147
Item 30: Use enums instead of
int
constants. . . . . . . . . . . . . . . . . 147
Item 31: Use instance fields instead of ordinals . . . . . . . . . . . . . . . 158
Item 32: Use
EnumSet
instead of bit fields . . . . . . . . . . . . . . . . . . . 159
Item 33: Use
EnumMap
instead of ordinal indexing. . . . . . . . . . . . . 161
Item 34: Emulate extensible enums with interfaces . . . . . . . . . . . . 165
Item 35: Prefer annotations to naming patterns . . . . . . . . . . . . . . . 169
Item 36: Consistently use the
Override
annotation. . . . . . . . . . . . 176
Item 37: Use marker interfaces to define types . . . . . . . . . . . . . . . 179
7 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Item 38: Check parameters for validity . . . . . . . . . . . . . . . . . . . . . 181
Item 39: Make defensive copies when needed . . . . . . . . . . . . . . . . 184
Item 40: Design method signatures carefully . . . . . . . . . . . . . . . . . 189
Item 41: Use overloading judiciously . . . . . . . . . . . . . . . . . . . . . . . 191
CONTENTS
xi
Item 42: Use varargs judiciously . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Item 43: Return empty arrays or collections, not nulls . . . . . . . . . 201
Item 44: Write doc comments for all exposed API elements . . . . 203
8 General Programming . . . . . . . . . . . . . . . . . . . . . . . . .209
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 . . . . .
209
212
215
218
221
224
227
228
230
233
234
237
9 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241
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 . . . . . . . . . . . . . . . . . . . . . . . . .
241
244
246
248
250
252
254
256
258
10 Concurrency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259
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
. . . . . . .
259
265
271
273
Plik z chomika:
musli_com
Inne pliki z tego folderu:
Big Java Late Objects [Horstmann 2012-02-01].pdf
(167477 KB)
Data Structures_ Abstraction and Design using Java (2nd ed.) [Koffman & Wolfgang 2010-01-26].pdf
(190252 KB)
Big Java Early Objects (5th ed.) [Horstmann 2013-01-04].pdf
(145099 KB)
Data Abstraction and Problem Solving with Java_ Walls and Mirrors (3rd ed.) [Prichard & Carrano 2010-10-30] (photocopier quality).pdf
(110506 KB)
A Little Java, a Few Patterns [Felleisen & Friedman 1997-12-19].pdf
(14847 KB)
Inne foldery tego chomika:
3D Design - Programming
ActionScript
Actionscript - Flash - Flex - Air
Ada
ADO
Zgłoś jeśli
naruszono regulamin