Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Joshua Bloch

Rating No ratings yet

本书介绍了在Java编程中78条极具实用价值的经验规则,这些经验规则涵盖了大多数开发人员每天所面临的问题的解决方案。通过对Java平台设计专家所使用的技术的全面描述,揭示了应该做什么,不应该做什么才能产生清晰、健壮和高效的代码。 本书中的每条规则都以简短、独立的小文章形式出现,并通过例子代码加以进一步说明。本书内容全面,结构清晰,讲解详细。可作为技术人员的参考用书。

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A rule-by-rule field manual for writing Java that other people can actually maintain: 78 short, opinionated items on objects, generics, enums, methods, and concurrency. Best for working Java developers who already know the syntax and now want to stop shipping subtly broken code. 【Book Arc】 - **Opening (~0%–10%)**: Frames the whole method — each item is a self-contained rule backed by example code — and immediately dives into object creation: static factories vs. constructors, the telescoping-constructor problem, Builder, and Singleton (including the single-element enum). - **Early (~10%–33%)**: The universal-method core. Deep treatment of `equals`/`hashCode` contracts (symmetry, transitivity, consistency), `toString`, and the many traps of `clone` and `finalize`, with the recurring lesson that violating these contracts breaks `HashMap`, `HashSet`, and friends. - **Middle (~33%–57%)**: Class and interface design — minimizing accessibility, avoiding exposed mutable fields, favoring composition over inheritance, designing and documenting for inheritance (or forbidding it), plus `Comparable`, constant interfaces, and skeletal implementations. - **Late (~57%–80%)**: The Java 5 feature block — generics, enums, annotations, autoboxing, varargs, and for-each — taught as patterns and idioms rather than syntax, with emphasis on erasure, bounded wildcards, and avoiding accidental boxing costs. - **Ending (~80%–100%)**: Method design (parameters, return values, signatures, documentation), then general programming details — local variables, control structures, library use, reflection and native methods, naming conventions, and optimization. The excerpts do not cover the serialization and concurrency items in detail. 【Key Takeaways】 - **Prefer static factories over public constructors when naming or caching matters** (Opening): they allow meaningful names, instance reuse, and return-type flexibility, and they underpin service-provider frameworks like JDBC. - **Builder beats telescoping constructors and JavaBeans for many optional parameters** (Opening): it keeps construction atomic and the object consistent, unlike step-by-step setters that leave objects temporarily invalid. - **The single-element enum is the best Singleton** (Opening): concise, serialization-safe, and immune to reflection attacks — a rare case where a newer idiom cleanly replaces older ones. - **`equals` and `hashCode` are a package deal with real contracts** (Early): breaking symmetry, transitivity, or consistency silently corrupts hash-based collections; a constant `hashCode` is legal but degrades hash tables to linked lists. - **`clone` and `finalize` are best avoided** (Early): `Cloneable` is flawed, deep copies risk stack overflow if done recursively, and copy constructors/conversion constructors are usually the better tool. - **Design for inheritance deliberately, or prohibit it** (Middle): constructors must never call overridable methods, protected members are permanent API commitments, and you should write subclasses before releasing an extensible class. - **Minimize accessibility and never expose mutable fields** (Middle): package-private is the default target; public immutable fields are a lesser evil, but `Point`/`Dimension`-style exposure is a cautionary tale. - **Generics, enums, and annotations are patterns, not just syntax** (Late): the payoff comes from idioms — bounded wildcards, skeletal implementations, enum-based constants — that keep code simple while gaining type safety. 【Reading Tips】 - Treat it as a reference, not a novel: read the item titles first, then deep-read the items touching code you currently own (object creation, `equals`/`hashCode`, generics). - Skim the Java 5 feature chapters if you already use generics and enums daily, but slow down on erasure, wildcards, and autoboxing pitfalls — those are where experienced developers still slip. - For each rule, ask "what breaks if I ignore this?" The book's power is in the failure modes, not the prescriptions. - Keep the item numbers as a shared vocabulary with your team; citing "Item 9" in code review is faster than re-arguing the design. - The serialization and concurrency material is only lightly represented in these excerpts — consult the full book for those chapters. 【Coverage Limits】 This guide is built from stratified excerpts covering object creation, universal methods, class/interface design, generics/enums/annotations, and method design; serialization, concurrency, and the appendix's version comparison are not covered in detail here.
Page 16
a Databse Connectivity)API。服务提 供者框架是指这样一个系统:多个服务提供者实现一个服务,系 统为服务提供者的客户端提供多个实现,并把他们从多个实现中 解耦出来。 服务提供者框架中有三个重要的组件:服务接口(Service Interface),这是提供者实现的;提供者注册API(Pro...
View in text
Excerpt 2
子类的终结方法 是否调用 super.finalize 并不重要。对 于每一个带有终结方法的非 final 公 有类,都应该考虑使用这种方法。 总之,除非是作为安全网,或者是为 了终止非关键的本地资源,否则请不 要使用终结方法。在这些很少见的情 况下,既然使用了终结方法,就要记 住调用 super.finaliz...
View in text
Excerpt 3
e , 并且它的超类都提供行为良好的 Clone 方 法。你从 super.clone() 中得到的对象可能 会接近于最终要返回的对象,也可能相差 甚远,这要取决于这个类的本质。从每个 超类的角度来看,这个对象僵尸原始对象 功能完整的克隆(clone)。在这个类中声 明的域(如果有的话)将等同于被克隆对 象中相应...
View in text
Excerpt 4
实习类。对 于处在它的包外部的客户端而言,不可变类实际上是 final 的,因为不可能把来自另一个包的类、缺少公有 的活受保护的构造器的类进行扩展。除了允许多个实现 类的灵活性之外,这种方法还使得有可能通过改善静态 工厂的对象缓存能力,在后续的发行版本中改进该类的 性能。 的方法: add 和 addAll ,因...
View in text
Excerpt 5
并不 好,因为客户端将无法传递任何其他的比较策略。 相反,我们需要定义一个 Comparator 接口,并修改 StringLengthComparator 来实现这个接口。换句话说, 我们在设计具体的策略类时,还需要定义一个 策略 接口(strategy interface) ,如下所示: // Strateg...
View in text
Excerpt 6
上调用 String 的 toUpperCase 方法,并不需要显式的转换,并且会确保 自动生成的转换会成功: // Little program to exercise our generic Sta ck public static void mai (String[] args) { Stack<Strin...
View in text
Excerpt 7
;如果它只是被用在一个特定的顶层类 中,它就应该成为该顶层类的一个成员类(见第22条)。例 如, java.math.RoundingMode 枚举表示十进制小数的舍入模式 (rounding mode)。这些舍入模式用于 BigDecimal 类,但 是它们提供了一个非常有用的抽象,这种抽象本质上又不属 于 B...
View in text
Excerpt 8
例,基本类型的实例却不是扩展类型的元素,这样很是混乱。目 前还没有很好的方法来枚举基本类型的所有元素及其扩展。最终,可 伸缩性会导致设计和实现的许多方面变得复杂起来。 也就是说,对于可伸缩的枚举类型而言,至少有一种具有说服力的用 例,这就是 操作吗(operation code) ,也称作 opcode 。操作吗...
View in text
Tags
AI categories
JavaProgrammingSoftware
Publish Year: 2008
Language: English
File Format: PDF
File Size: 2.1 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…