<<Effective Java>> note&staff in 2009

Chapte 2:when and how to create objects,when and how to avoid creating objects

 

Item 1:Consider static factory methods instead of constructors{

 

};

-------------------------Beautiful  Cutline-------------------------

 

Item 2:Consider a builder when faced with many constructor parameters{

 Method 1#:Constructor pattern

 Method 2#:JavaBean     pattern

 Method 3#:SubClass     pattern

};

 

-------------------------Beautiful  Cutline-------------------------

Item 3:Enforce the singleton property with a private constructor or an enum type{

Before release 1.5,there were two ways.

way 1:

pulbic class Elvis{
  public static fina Elvis INSTANCE=new Elvis();
  private Elvis(){...}
}

Disadvantage:A privileaed client can invoke the private constructor reflectively with the adi of the AccessibleObject.setAccessible method.

 

way 2:

pulbic class Elvis{
  private static fina Elvis INSTANCE=new Elvis();
  private Elvis(){...}
  public static Elvis getInstance(){...}
}

Disadvantage:with the same caveat above.

Advantage:Make it clear that the class is singleton.

 

As of release 1.5:the best way to declare singleton is with Enum

way 3:

public enum Elvis{
  INSTANCE;
}

-------------------------Beautiful  Cutline-------------------------

 

Item 4: Enforce noninstantiablility with a private constructor.

  Attempting to enforce noninstantiability by making a class abstract does not work.the class can be subclassed and the subclass instantiated.

 

-------------------------Beautiful  Cutline------------------------- 

Item 5:Avoid creating unneccessary objects

  Avoid creating objects when using final class

  Reusing mutable objects if you know they won't be modified.

  Prefer primitives to boxed primitives.

 

-------------------------Beautiful  Cutline-------------------------

Item 6:Eliminate obsolete object references

  Whenever a class manage own memory,programmer should be alert for memory leaks.

  Whenever an element is freed,any object reference contained in the element should be nulled out.

 

-------------------------Beautiful  Cutline-------------------------

Item 7: Avoid finalizers

  It maybe happen that the finalizer thread will never run as the priority is low.

  Don't use finalizers except as a safty net or to terminate noncritical native resources.In those rare instances where you do use a finalizer,remember to invoke super.finalize.

 

-------------------------Intentional Cutline-------------------------

 

Chapt 3:Methods Common to All Objects

 

Item 8: Obey the general contract when overriding equals

  The easist way to avoid problems is not to overide the equals method,in which case echo instance of the class is equal only to itself.

 

Item9: Always override hashCode when you override equals

Chapter 7 Methods

 

Item 38 Check parameters for validity

在函数开始前作必要的检查工作,利用if或assert语句

Item 39 make defensive copies when needed

对于类的成员变量赋值工作要非常的小心,不要轻易暴露类的内部成员变量,充分考虑client端的信任问题,书写安全的代码。

Item 41 use overloading judiciously

你可以使用重载但并不意味着你应该使用,一旦你使用重载你需要确认重载函数的行为像你预期的一样工作,不然往往会发生诡异的问题。

item 42 use varargs judiciouly

不要滥用多参数,即便用了,也需要做一定的检查工作。

item 43 return empty arrays or collections, not nulls

当函数返回值为集合的时候,最好不要返回null,而返回空集合,这样也省去了上层判空检查,使代码更高效,重构的时候搜索下有没有return null这种语句。

item 44 write doc comments for all exposed api elements.

 

chapter 5 generics

item 23 don't use raw types in new code.

item 24 eliminate unchecked warnings.

item 25 prefer lists to arrays List集合在编译期就进行类型检查,而数组在运行期。

item 26 favor generic types

item 27 favor generic methods

 

chapter 8

item45 minimize the scope of local variables--

item46 prefer for-echo loops to traditional for loops-更简洁和高效

item47 know and use the libraries-不要重复造已有的轮子。

item48 avoid float and double if exact answers are required-使用BigDecimal

item49 prefer primitive types to boxed primitives-基本类型更简单更快

item50 avoid strings where other types are more appropriate

item51 beware the performance of string concatenation-string是个immutable类型的

item52 refer to objects by their interface-用接口定义,提高可扩展性

item53 prefer interfaces to reflection-反射式个好工具,但他的缺点是你放弃了所有编译器检查,牺牲了效率,代码的可读性

item54 use native method judiciously 减弱了可调试性、可读性、非平台无关的

item55 optimize judiciously

item56 adhere to generally accepted naming conventions

 

chapter 10

item66 synchronize access to shared mutable data

item67 avoid excessive synchronization-防止死锁和数据不正确,尽量少用同步

item68 prefer executors and tasks to threads.1.5提供了一套执行框架(executor framework),提供了一整套线程池解决方案。

item69 prefer concurrency utilities to wait and notify.更改传统的线程间通讯方式,利用一些CountDownLatch,CyclicBarrier。

item71 use lazy initialization judiciously-对于静态实例,采用私有静态类实现延迟加载,对于一般实例,可使用double-check or single-check 来实现。

item72 don't depend on the thread scheduler-不要依靠系统来管理线程执行情况,利用thread的sleep方法来代替yield,线程的优先级别也不太可靠。

item73 avoid thread groups-线程组管理目前已经不太提倡使用了,他提供的函数没有太大的意义,建议不再使用。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值