Tài liệu Addison Wesley - Effective Java Programming Language Guide pptx

180 603 0
Tài liệu Addison Wesley - Effective Java Programming Language Guide pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

[...].. .Effective Java: Programming Language Guide This book uses a few technical terms that are not defined in The Java Language Specification The term exported API, or simply API, refers to the classes, interfaces, constructors, members, and serialized forms by which a programmer accesses a class, interface, or package (The term API, which is short for application programming interface,... These conventions are still evolving, but two names for static factory methods are becoming common: 10 Effective Java: Programming Language Guide • • valueOf— Returns an instance that has, loosely speaking, the same value as its parameters Static factory methods with this name are effectively type-conversion operators getInstance— Returns an instance that is described by its parameters but cannot be... fallen into disuse This cleaning can be done by a background thread (perhaps via the java. util.Timer API) or as a side effect of adding new entries to the cache The java. util.LinkedHashMap class, added in release 1.4, facilitates the latter approach with its removeEldestEntry method 18 Effective Java: Programming Language Guide Because memory leaks typically do not manifest themselves as obvious failures,... object, a necessary counterpart to constructors In the Java programming language, the garbage collector reclaims the storage associated with an object when it becomes unreachable, requiring no special effort on the part of the programmer C++ destructors are also used to reclaim other nonmemory resources In the Java programming language, the try-finally block is generally used for this purpose There... consider the following class: /** * Case-insensitive string Case of the original string is * preserved by toString, but ignored in comparisons */ public final class CaseInsensitiveString { private String s; public CaseInsensitiveString(String s) { if (s == null) throw new NullPointerException(); this.s = s; } 24 Effective Java: Programming Language Guide //Broken - violates symmetry! public boolean equals(Object... ColorPoint(1, 2, Color.RED); 26 Effective Java: Programming Language Guide Then p.equals(cp) returns true, while cp.equals(p) returns false You might try to fix the problem by having ColorPoint.equals ignore color when doing “mixed comparisons”: //Broken - violates transitivity public boolean equals(Object o) { if (!(o instanceof Point)) return false; // If o is a normal Point, do a color-blind comparison if... color; } /** * Returns the point-view of this color point */ public Point asPoint() { return point; } 27 Effective Java: Programming Language Guide public boolean equals(Object o) { if (!(o instanceof ColorPoint)) return false; ColorPoint cp = (ColorPoint)o; return cp.point.equals(point) && cp.color.equals(color); } } // Remainder omitted There are some classes in the Java platform libraries that subclass... that adds a new aspect to its superclass In other words, the subclass adds a piece of information that affects equals comparisons Let's start with a simple immutable two-dimensional point class: 25 Effective Java: Programming Language Guide public class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public boolean equals(Object o) { if (!(o instanceof... support Not coincidentally, they are also the elements for which the Javadoc utility generates documentation in its default mode of operation Loosely speaking, the exported API of a package consists of the public and protected members and constructors of every public class or interface in the package 7 Effective Java: Programming Language Guide Chapter 2 Creating and Destroying Objects This chapter concerns... the Java Cryptography Extension (JCE) A service provider framework is a system wherein providers make multiple implementations of an API available to users of the framework A mechanism is provided to register these implementations, making them available for use Clients of the framework use the API without worrying about which implementation they are using 9 Effective Java: Programming Language Guide . alt="" Effective Java: Programming Language Guide Joshua Bloch Publisher: Addison Wesley First Edition June 01, 2001 ISBN: 0-2 0 1-3 100 5-8 , 272. grammar of the Java Programming Language, including The Java Programming Language by Arnold, Gosling, and Holmes [Arnold00] or The Java Language Specification

Ngày đăng: 16/01/2014, 20:20

Từ khóa liên quan

Mục lục

  • Cover

  • Table of Contents

  • Foreword

  • Preface

  • Acknowledgments

  • 1. Introduction

  • 2. Creating and Destroying Objects

    • Item 1: Consider providing static factory methods instead of constructors

    • Item 2: Enforce the singleton property with a private constructor

    • Item 3: Enforce noninstantiability with a private constructor

    • Item 4: Avoid creating duplicate objects

    • Item 5: Eliminate obsolete object references

    • Item 6: Avoid finalizers

  • 3. Methods Common to All Objects

    • Item 7: Obey the general contract when overriding equals

    • Item 8: Always override hashCode when you override equals

    • Item 9: Always override toString

    • Item 10: Override clone judiciously

    • Item 11: Consider implementing Comparable

  • 4. Classes and Interfaces

    • Item 12: Minimize the accessibility of classes and members

    • Item 13: Favor immutability

    • Item 14: Favor composition over inheritance

    • Item 15: Design and document for inheritance or else prohibit it

    • Item 16: Prefer interfaces to abstract classes

    • Item 17: Use interfaces only to define types

    • Item 18: Favor static member classes over nonstatic

  • 5. Substitutes for C Constructs

    • Item 19: Replace structures with classes

    • Item 20: Replace unions with class hierarchies

    • Item 21: Replace enum constructs with classes

    • Item 22: Replace function pointers with classes and interfaces

  • 6. Methods

    • Item 23: Check parameters for validity

    • Item 24: Make defensive copies when needed

    • Item 25: Design method signatures carefully

    • Item 26: Use overloading judiciously

    • Item 27: Return zero-length arrays, not nulls

    • Item 28: Write doc comments for all exposed API elements

  • 7. General Programming

    • Item 29: Minimize the scope of local variables

    • Item 30: Know and use the libraries

    • Item 31: Avoid float and double if exact answers are required

    • Item 32: Avoid strings where other types are more appropriate

    • Item 33: Beware the performance of string concatenation

    • Item 34: Refer to objects by their interfaces

    • Item 35: Prefer interfaces to reflection

    • Item 36: Use native methods judiciously

    • Item 37: Optimize judiciously

    • Item 38: Adhere to generally accepted naming conventions

  • 8. Exceptions

    • Item 39:Use exceptions only for exceptional conditions

    • Item 40:Use checked exceptions for recoverable conditions and run-time exceptions for programming errors

    • Item 41:Avoid unnecessary use of checked exceptions

    • Item 42:Favor the use of standard exceptions

    • Item 43: Throw exceptions appropriate to the abstraction

    • Item 44:Document all exceptions thrown by each method

    • Item 45:Include failure-capture information in detail messages

    • Item 46:Strive for </vetbfailure atomicity

    • Item 47:Don't ignore exceptions

  • 9. Threads

    • Item 48: Synchronize access to shared mutable data

    • Item 49: Avoid excessive synchronization

    • Item 50: Never invoke wait outside a loop

    • Item 51: Don't depend on the thread scheduler

    • Item 52: Document thread safety

    • Item 53: Avoid thread groups

  • 10. Serialization

    • Item 54: Implement Serializable judiciously

    • Item 55:Consider using a custom serialized form

    • Item 56:Write readObject methods defensively

    • Item 57: Provide a readResolve method when necessary

  • References

Tài liệu cùng người dùng

Tài liệu liên quan