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的更多相关文章

  1. Effective Java 23 Don't use raw types in new code

    Generic types advantage Parameterized type can provide erroneous check in compile time. // Parameter ...

  2. Effective Java 67 Avoid excessive synchronization

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

  3. Effective Java 07 Avoid finallizers

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

  4. 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 ...

  5. Effective Java 73 Avoid thread groups

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

  6. Effective Java 05 Avoid creating unnecessary objects

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

  7. 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 ...

  8. 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 ...

  9. 《Effective Java》读书笔记 - 8.通用编程

    Chapter 8 General Programming Item 45: Minimize the scope of local variables local variables应该在他们要被用 ...

随机推荐

  1. js-初步了解

    一.javascript的由来 Javascript是一种web技术,最初起名叫LiveScript,它是Netscape开发出来一种脚本语言,其目的是为了扩展基本的Html的功能,用来替代复杂的CG ...

  2. javascript之纯数字验证

    现在有一个需求如下图: 产品经理说Card Number只能让输入数字(中间的空格是格式自加的,也是用js实现的),有时候我脑海中出现了个声音,啥玩意,加个type=number不就行了,事实发现图样 ...

  3. spring.net中的IOC和DI-初使用

    前面准备:下载spring.net并解压 下载地址:spring.net下载地址 Ioc:控制反转         DI:依赖注入 一.IOC(控制反转) 1.新建一个控制台程序springTest, ...

  4. Worm.Win32.DownLoader.ns病毒主进程新式输入法注入分析(IME Inject)

    1.病毒会在system32目录生成一个以tmp结尾的随机数命名的文件. 2.然后挂钩HOOK本进程空间的imm32.dll导出的ImmLoadLayout函数和ntdll.dll导出的ZwQuery ...

  5. 百度地图js根据经纬度定位和拖动定位点

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  6. 在uwp仿制WPF的Window

    移植WPF软件到uwp时碰到用作对话框的Window有多种处理选择.我个人认为最省事的是用ContentDialog模拟Window. 比如你想把上面这个WPF窗体弄到uwp里面去 1.修改Conte ...

  7. Titanium开发环境搭建第二个坑

    1. build时总提示  --key-password <keypass> 参数没传,不填又说密码不对,填对了又说没传,应该是ide的问题,暂时不知怎样去设置该命令参数: 2. 继续去T ...

  8. Android性能优化之一:ViewStub

    ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...

  9. ThoughtWorks西邮暑期特训营 -- JavaScript在线笔试题

    ThoughtWorks 公司在西邮正式开办的只教女生前端开发的女子卓越实验室已经几个月过去了,这次计划于暑期在西邮内部开展面向所有性别所有专业的前端培训. 具体官方安排请戳:ThoughtWorks ...

  10. 设计模式之Builder (创建者模式)的一些个人理解(转)

    对于Builder模式很简单,但是一直想不明白为什么要这么设计,为什么要向builder要Product而不是向知道建造过程的Director要.刚才google到一篇文章,总算清楚了.在这里转贴一下 ...