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. 内存中OLTP(Hekaton)里的事务日志记录

    在今天的文章里,我想详细讨论下内存中OLTP里的事务日志如何写入事务日志.我们都知道,对于你的内存优化表(Memory Optimized Tables),内存中OLTP提供你2个持久性(durabi ...

  2. struts2基础——自定义拦截器

    一.自定义拦截器 默认的拦截器能实现的功能是有限的,Struts2 支持自定义拦截器. 二.拦截器类 1.实现 Interceptor 接口 2.继承 AbstractInterceptor 抽象类, ...

  3. P6 EPPM Installation and Configuration Guide 16 R1 April 2016

    P6 EPPM Installation and Configuration Guide 16 R1         April 2016 Contents About Installing and ...

  4. 使用BOM 的window对象属性打开新窗口

    ★  示例1 要求:弹出新窗口,并向新窗口写入动态HTML代码 代码 <buttononclick="btnOpen()">打开新窗口</button> & ...

  5. Chrome弹窗的简单应用(选择结构与循环结构)

    ★选择结构★ ★JS实现弹窗显示随机数 示例代码效果图   ★ 弹窗实现对随机数的进一步判断 示例代码效果图 ★综合应用   比较大小 ★ 判断成绩等级 ): : : : : alert(" ...

  6. 重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片

    [源码下载] 重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片 作者:webabcd 介绍重新想象 Win ...

  7. [PE结构分析] 6.IMAGE_SECTION_HEADER

    IMAGE_SECTION_HEADER 的源代码如下: typedef struct _IMAGE_SECTION_HEADER { BYTE Name[IMAGE_SIZEOF_SHORT_NAM ...

  8. PHP学习笔记:数据库学习心得

    存储过程: 存储过程(Stored Procedure)是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库.中用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它. 因为语 ...

  9. Python函数:一个简单的迭代

    #!/usr/bin/env python # -*- coding: utf-8 -*- def fact(n): if n == 1 : return 1 return n * fact(n-1) ...

  10. JVM的ClassLoader过程分析

    本文来自网络:深入分析Java ClassLoader原理 http://my.oschina.net/zhengjian/blog/133836 一. JVM的ClassLoader过程以及装载原理 ...