Effective Java 05 Avoid creating unnecessary objects
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
- 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.
- Prefer primitives to boxed primitives, and watch out for unintentional autoboxing.
Effective Java 05 Avoid creating unnecessary objects的更多相关文章
- Java之创建对象>5.Avoid creating unnecessary objects
String s = new String("stringette"); // DON'T DO THIS! The improved version is simply the ...
- 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 ...
- Effective Java Methods Common to All Objects
Obey the general contract when overriding equals 先了解equals 和 == 的区别,如果不重写equals, equals比较的仅仅只是是否引用同一 ...
- Effective Java 07 Avoid finallizers
NOTE Never do anything time-critical in a finalizer. Never depend on a finalizer to update critical ...
- 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 ...
- Effective Java 67 Avoid excessive synchronization
Principle To avoid liveness and safety failures, never cede control to the client within a synchroni ...
- Effective Java 73 Avoid thread groups
Thread groups were originally envisioned as a mechanism for isolating applets for security purposes. ...
- 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 ...
- 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 ...
随机推荐
- flex布局浅谈和实例
阿基米德曾说给我一个支点我可以撬动地球,而拥有flex基本可以撬动所有的布局. 1.flex布局基本介绍及效果展示 工欲善其事必先利其器,来来来,一起看下基础知识先(呵~,老掉牙,但是有用啊). ** ...
- [C#] 谈谈异步编程async await
为什么需要异步,异步对可能起阻止作用的活动(例如,应用程序访问 Web 时)至关重要. 对 Web 资源的访问有时很慢或会延迟. 如果此类活动在同步过程中受阻,则整个应用程序必须等待. 在异步过程中, ...
- ASP.NET MVC5---通过QueryString传值
1.首先我们来看看普通的传值是啥样的.如图所示,我们普通的传值是这样的 public ActionResult Edit(int?id) { if (id == null) { return new ...
- 组合数学 - 母函数的运用 + 模板 --- hdu : 2082
找单词 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 周末web前端练习
在 CSS 样式定义中,以下哪种 RGB 颜色值是 Web 安全色? A]#111111B]#222222C]#333333D]#444444 答案:http://hovertree.com/ti ...
- 在node.js中使用COOKIE
node.js中如何向客户端发送COOKIE呢?有如下两个方案: 一.使用response.writeHead,代码示例: //设置过期时间为一分钟 var today = new Date(); v ...
- 关于WPF绘图中的path.data在后台重新赋值的语法
//XAML语法 <Path Name="path_M" Fill="LawnGreen" Data="M 0 0 L 100 0 L 100 ...
- java操作小技巧,遇到过的会一直更新,方便查找
1.<c:forEach>可以循环map array List 2.操纵数组,不知道类型的情况下,不需要判断数组类型,直接用反射,arrays.Class.isArrays() 获取数组长 ...
- Linux FTP配置文件说明
一.vsftpd说明: LINUX下实现FTP服务的软件很多,最常见的有vsftpd,Wu-ftpd和Proftp等.Red Hat Enterprise Linux中默认安装的是vsftpd. 访问 ...
- android onNewIntent
在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是当您需要让后台运行的Activity回到前台并传递一些数据可能就 ...