String s = new String("stringette"); // Don't do this. This will create an object each time

Vs

String s = "stringette";

class Person {

private final Date birthDate;

// Other fields, methods, and constructor omitted

/**

* The starting and ending dates of the baby boom.

*/

private static final Date BOOM_START;

private static final Date BOOM_END;

static {

Calendar gmtCal =

Calendar.getInstance(TimeZone.getTimeZone("GMT"));

gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);

BOOM_START = gmtCal.getTime();

gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);

BOOM_END = gmtCal.getTime();

}

public boolean isBabyBoomer() {

return birthDate.compareTo(BOOM_START) >= 0 &&

birthDate.compareTo(BOOM_END) < 0;

}

}

NOTE

  1. When using Adapter pattern don't created an new object of the backing object since there is no need for the specific state of the Adapter object.
  2. Prefer primitives to boxed primitives, and watch out for unintentional autoboxing.

Effective Java 05 Avoid creating unnecessary objects的更多相关文章

  1. Java之创建对象>5.Avoid creating unnecessary objects

    String s = new String("stringette"); // DON'T DO THIS! The improved version is simply the ...

  2. Effective Java 59 Avoid unnecessary use of checked exceptions

    The burden is justified if the exceptional condition cannot be prevented by proper use of the API an ...

  3. Effective Java Methods Common to All Objects

    Obey the general contract when overriding equals 先了解equals 和 == 的区别,如果不重写equals, equals比较的仅仅只是是否引用同一 ...

  4. Effective Java 07 Avoid finallizers

    NOTE Never do anything time-critical in a finalizer. Never depend on a finalizer to update critical ...

  5. Effective Java 50 Avoid strings where other types are more appropriate

    Principle Strings are poor substitutes for other value types. Such as int, float or BigInteger. Stri ...

  6. Effective Java 67 Avoid excessive synchronization

    Principle To avoid liveness and safety failures, never cede control to the client within a synchroni ...

  7. Effective Java 73 Avoid thread groups

    Thread groups were originally envisioned as a mechanism for isolating applets for security purposes. ...

  8. Effective Java 48 Avoid float and double if exact answers are required

    Reason The float and double types are particularly ill-suited for monetary calculations because it i ...

  9. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

随机推荐

  1. [git]fork+pull提交模式

    fork+pull提交模式 在公司项目中,大多都是通过"主题分支"的方式,进行开发与合并代码.但是,这样又一个弊端就是:合并代码后需要删除分支.同时,如果是开源的项目的话,非项目中 ...

  2. Android 学习笔记之ContentProvider实现数据共享....

    PS:最近听老师说打算让我参与企业的app制作,让我加快学习的进度...好吧,貌似下周还有考试...貌似实验室这个app也要做...暂时不管了...那就只能加快进度了,感觉略微的有点激动和紧张,总算是 ...

  3. Java多线程学习笔记——信号量的使用

    Java中在控制多线程访问资源的时候使用了信号量可以控制多个线程同时访问一个资源. 有两个构造方法: public Semaphore(int permits) public Semaphore(in ...

  4. Week1项目报告

    1. 预测时间 Personal Software Process Stages Time(h) 计划 · 估计这个任务需要多少时间 16.5 开发 · 需求分析 (包括学习新技术) 4 · 生成设计 ...

  5. sprint3(第三天)

    今天在做的任务是整合前台和后台,使前台可以从后台得到数据 燃尽图:

  6. Scrum团队成立

    1.团队名称:瘦子不吃肥肉 目标:学习更多,了解更多! 口号:加油 团队照: 2.角色分配: 产品负责人:卓嘉炜        http://www.cnblogs.com/luoliuxi/ Scr ...

  7. C#开源资源项目

    一.AOP框架 Encase 是C#编写开发的为.NET平台提供的AOP框架.Encase 独特的提供了把方面(aspects)部署到运行时代码,而其它AOP框架依赖配置文件的方式.这种部署方面(as ...

  8. 【.NET框架】Dapper ORM 用法—Net下无敌的ORM

    假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载 Dapper的优势: 1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编 ...

  9. windbg学习进阶之——windbg字段名及其意义

    要使用windbg分析dump必须加载正确的符号,可以通过设置Symbols File Path为"D:/Symbols;SRV*D:/Symbols*http://msdl.microso ...

  10. 用PHP实现Windows域验证

    系统集成中,可能会有这种需求 Windows 域验证本质上是LDAP验证 但在网上居然找不到详细的技术文档,可见不受待见之极.