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.
- Strings are poor substitutes for enum types. As discussed in Item 30.
- Strings are poor substitutes for aggregate types. A better approach is simply to write a class to represent the aggregate, often a private static member class (Item 22).
// Inappropriate use of string as aggregate type
String compoundKey = className + "#" + i.next();
- Strings are poor substitutes for capabilities(unforgeable key).
Client-provided string keys are used to identify each thread-local variable:
// Broken - inappropriate use of string as capability!
public class ThreadLocal {
private ThreadLocal() { } // Noninstantiable
// Sets the current thread's value for the named variable.
public static void set(String key, Object value);
// Returns the current thread's value for the named variable.
public static Object get(String key);
}
The root cause: the string keys represent a shared global namespace for thread-local variables.
public class ThreadLocal {
private ThreadLocal() { } // Noninstantiable
public static class Key { // (Capability)
Key() { }
}
// Generates a unique, unforgeable key
public static Key getKey() {
return new Key();
}
public static void set(Key key, Object value);
public static Object get(Key key);
}
It is a simple, faster and more elegant matter to make this API typesafe by generifying the ThreadLocal class (Item 26):
public final class ThreadLocal<T> {
public ThreadLocal() { }
public void set(T value);
public T get();
}
Summary
Avoid the natural tendency to represent objects as strings when better data types exist or can be written. Used inappropriately, strings are more cumbersome, less flexible, slower, and more error-prone than other types. Types for which strings are commonly misused include primitive types, enums, and aggregate types.
Effective Java 50 Avoid strings where other types are more appropriate的更多相关文章
- Effective Java 23 Don't use raw types in new code
Generic types advantage Parameterized type can provide erroneous check in compile time. // Parameter ...
- Effective Java 67 Avoid excessive synchronization
Principle To avoid liveness and safety failures, never cede control to the client within a synchroni ...
- Effective Java 07 Avoid finallizers
NOTE Never do anything time-critical in a finalizer. Never depend on a finalizer to update critical ...
- 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 73 Avoid thread groups
Thread groups were originally envisioned as a mechanism for isolating applets for security purposes. ...
- Effective Java 05 Avoid creating unnecessary objects
String s = new String("stringette"); // Don't do this. This will create an object each tim ...
- 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 Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
- 《Effective Java》读书笔记 - 8.通用编程
Chapter 8 General Programming Item 45: Minimize the scope of local variables local variables应该在他们要被用 ...
随机推荐
- 初遇sql server
今天初始接触sql server 和mysql的语法有一些不同 sql server中使用[] 或双引号来表示数据库.字段名.表名等,而字符串使用单引号来表示 mysql中数据库名,表名,字段名不需要 ...
- 3D拓扑自动布局之Node.js篇
上篇将3D弹力布局的算法运行在Web Workers后台,这篇我们将进一步折腾,将算法运行到真正的后台:Node.js,事先申明Node.js篇和Web Workers篇一样,在这个应用场景下并不能提 ...
- Sprint回顾-0525
1.回顾组织 主题:“我们下次怎么样才能更加认真对待?” 时间:设定为1小时. 参与者:整个团队. 场所:宿舍走廊. 秘书:团队队长秘书,筹备.记录.整理. 2.回顾流程 Sprint总结: ...
- Python入门笔记(15):对文件的操作(1)
一.文件对象 我理解的文件对象就是一个接口,通过这个接口对文件进行相关操作. <Python 核心编程>上说的很晦涩,这里没有深刻理解到,希望有人能解释给我听. >>> ...
- MotoG2刷机小结
昨天,终于受不了MotoG2的后台软件不停重启,手机经常卡死,于是决定刷机,网上的教程好多,实践后总结一下,下面这个教程完美通过. 1.解官方BL锁.第一步:http://pan.baidu.com/ ...
- sencha panel的头header上添加刷新按钮
var plet3=Ext.create('portaltest3.view.Portlet', { title: '提醒', ...
- spring报nested exception is java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injected错误
http://www.oschina.net/question/1539472_159699
- 线段树——Ultra-QuickSort
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109331#problem/A Description In this prob ...
- struts2进阶篇(4)
一.使用ActionContext访问Servlet API strtus2提供了一个ActionContext类,该类别称为Action上下文或者Action环境,Action可以通过该类来访问最常 ...
- ajax案例源码
html文件中demo2_index.html ---------------------------------------------------------------------------- ...