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

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

Tài liệu liên quan