《Effective Java》2nd变更简评

《Effective Java》无疑是java领域的经典著作。第二版出来后,看了看,然后和第一版对比一下。

下面介绍第二版的变更,以及揣摩作者的意图。

删掉了原来的Substitutes for C constructs一章。


[b]C2 Creating and Destroying Objects[/b]

Item 1 Consider static factory methods instead of constructors.

Item 2 Consider a builder when faced with many constructor parameters.
新加的一个条目,这个条目介绍了Builer模式的用法,但是似乎和这本书的原有条目的特点不符合。而Builder一般在介绍设计模式的书籍里面会进行全面的介绍。放在这里有些不伦不类。有凑数之嫌。或许大师有其他的考虑。

Item 3 Enforce the singleton property with a private constructor or an enum type.
新加入了Enum type来实现Singleton。
从语法上讲,这样当然是可以的,但是谈及Enum对于程序员来说,第一反应一般还是说有几个并列的概念组成的一个枚举集合,一个Enum定义一个元素来实现Singleton怎么看都是怪怪的。貌似作者很爱Enum,在第二版多处用到了Enum。

Item 4 Enforce noninstantiability with a private constructor.

Item 5 Avoid creating unnecessary objects.
第一版的duplicate objects变成了unnecessary objects,这个虽然是一个很细小的变动,但是味道十足,慢慢品之。

Item 6 Eliminate obsolete object references.

Item 7 Avoid finalizers.


[b]C3 Methods Common to All Objects[/b]

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
没有变化,老样子。


[b]C4 Classes and Interfaces[/b]

Item 13: Minimize the accessibility of classes and members

Item 14: In public classes, use accessor methods, not public fields
新加的条目。

Item 15: Minimize mutability
原为Favor immutability. 现在改成了Minimize mutability,覆盖的范围更广一点。

Item 16: Favor composition over inheritance

Item 17: Design and document for inheritance or else prohibit it

Item 18: Prefer interfaces to abstract classes

Item 19: Use interfaces only to define types

Item 20: Prefer class hierarchies to tagged classes
新加的条目。解释了在类的设计中引入tag field的坏处,以及如何用类的继承体系去实现相似的功能。

Item 21: Use function objects to represent strategies
新加的条目。这个条目好像意义不大,讲解了java在没有原生函数指针的情况下如何用其他手段完成函数指针的功能。

Item 22: Favor static member classes over nonstatic


[b]C5 Generics[/b]

Item 23: Don't use raw types in new code
Item 24: Eliminate unchecked warnings
Item 25: Prefer lists to arrays
Item 26: Favor generic types
Item 27: Favor generic methods
Item 28: Use bounded wildcards to increase API flexibility
Item 29: Consider typesafe heterogeneous containers
这一章是新增的,主要介绍java5泛型的使用。讲解的比较好,主要是讲解怎么用,对原理基本不介绍。


[b]C6 Enums and Annotations[/b]

Item 30: Use enums instead of int constants
Item 31: Use instance fields instead of ordinals
Item 32: Use EnumSet instead of bit fields
Item 33: Use EnumMap instead of ordinal indexing
Item 34: Emulate extensible enums with interfaces
Item 35: Prefer annotations to naming patterns
Item 36: Consistently use the Override annotation
Item 37: Use marker interfaces to define types

这一章是新增的,主要介绍Enum和Annotation。
个人感觉作者对于Enum过于偏爱了,对于Enum有些过度使用了。例如实现复杂接口,考虑继承的实现等等。如果Enum用的这么复杂,用《java编程语言》一书的思想来说,应该考虑用类而不是Enum了。


[b]C7 Methods[/b]

Item 38: Check parameters for validity

Item 39: Make defensive copies when needed

Item 40: Design method signatures carefully

Item 41: Use overloading judiciously

Item 42: Use varargs judiciously
新的条目。

Item 43: Return empty arrays or collections, not nulls
加入了Collections.

Item 44: Write doc comments for all exposed API elements


[b]C8 General Programming[/b]

Item 45: Minimize the scope of local variables

Item 46: Prefer for-each loops to traditional for loops
新增条目,增加了对新的语言特性的for-each的讲解,以及什么时候使用这个特性比较好的说明。

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
新增条目。引入自动装箱拆箱是一个有利有弊的事情。大部分情况下还是Primitive比较好用。

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


[b]C9 Exceptions[/b]

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

没有变化,老样子。


[b]C10 Concurrency[/b]

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
Item 70: Document thread safety
Item 71: Use lazy initialization judiciously
Item 72: Don't depend on the thread scheduler
Item 73: Avoid thread groups

本章的标题从Threads变成Concurrency,体现了java对并发编程支持的进步。
删掉了老的Never invoke wait outside a loop。不是这个不重要,估计大家都知道了。
所以没有单列一个Item,在文中还是有提及的。
Item68,Item69,Item71为新增的。主要是对java5新加的concurrency做介绍。
但是不得不说,这个简介太蜻蜓点水了。不如本书的其它章节那么详尽深入。
作为java5一个重要的新特性,没有得到重视啊。有可能是concurrency太复杂,展开论述篇幅太大吧。


[b]C11 Serialization[/b]

Item 74: Implement Serializable judiciously
Item 75: Consider using a custom serialized form
Item 76: Write readObject methods defensively
Item 77: For instance control, prefer enum types to readResolve
作者对Enum真是偏爱啊,总觉得这么使用Enum有点别扭。
Item 78: Consider serialization proxies instead of serialized instances
新增条目,介绍一种安全简单的模式来系列化对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值