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应该在他们要被用 ...
随机推荐
- JS&CSS文件请求合并及压缩处理研究(三)
上篇我们进行了一些代码方面的准备工作.接下来的逻辑是:在View页面解析时,通过 Html.AppendResFile 方法添加的资源文件,我们需要按照分组.优先级,文件名等条件,对其路径进行合并.具 ...
- 从P6 EPPM 8 R3 到P6 EPPM 16 R1 有哪些改变?
Product 特征 First Release for Feature P6 EPPM 通过编辑活动标识替换关系.当你需要修改一个关系,你不需要删除现有的关系,并作出一个新的,你可以简单地编辑活动的 ...
- P6 EPPM R16.1安装与配置指南(一)
标题 http://www.cnblogs.com/endv/p/5634620.html 安装与配置指南安装与配置指南(数据库)说明哪些How to set up the P6专业数据库和服务器.a ...
- UnityShader快速上手指南(四)
简介 由于其他项目中断了几天更新,继续~~ 这一篇主要是讲光照的(包含漫反射和高光以及多光源的处理) 还是先来看看具体效果(多光源后面单独展示) 有了基本的光照处理之后越来越有立体感了有不有 ╮(╯▽ ...
- PostgreSQL avg()函数
PostgreSQL的AVG函数是用来找出各种记录中的一个字段的平均值. 为了理解AVG函数考虑表COMPANY 有如下记录: testdb# select * from COMPANY; id | ...
- 最小化安装centos7下配置网络
虚拟机操作系统:centos7.0 命令行模式 1.首先明确centos7在最小化安装完是不支持上网的,相应的查看网络以及修改网络参数是不能使用的,最常见的就是我们常用的ifconfig. 2.找到网 ...
- 利用javascript、php和ajax实现计算器
计算器和ajax部分: <?php /** * Created by PhpStorm. * User: Administrator * Date: 16-9-2 * Time: 上午9:20 ...
- Durandal介绍
Durandal是一个JS框架用于构建客户端single page application(SPAs).它支持MVC,MVP与MVVM前端构架模式.使用RequireJS做为其基本约定层,D ...
- MaterialRefreshLayout
以上就介绍了比SwipeRefreshLayout更漂亮和强大的下拉刷新控件:Android-MaterialRefreshLayout 1.xml <?xml version="1. ...
- Play 可以做的 5 件很酷的事
Play 可以做的 5 件很酷的事 本章译者:@Playframwork 通过 5 个实例,透视 Play 框架背后的哲学. 绑定 HTTP 参数到 JAVA 方法参数 用 Play 框架,在 Jav ...