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. 高效查看MySQL帮助文档的方法

    在mysql的使用过程中, 可能经常会遇到以下问题: 某个操作语法忘记了, 如何快速查找? 如何快速知道当前版本上某个字段类型的取值范围? 当前版本都支持哪些函数?希望有例子说明.. 当前版本是否支持 ...

  2. 第十篇 SQL Server代理使用代理帐户

    本篇文章是SQL Server代理系列的第十篇,详细内容请参考原文 在这一系列的上一篇,你查看了msdb库下用于授权访问SQL Server代理的安全角色.这些角色包括SQLAgentUserRole ...

  3. DZNEmptyDataSet,优秀的空白页或者出错页封装

    简介 项目主页:https://github.com/dzenbot/DZNEmptyDataSet 提示:主要用于UITableView和UICollectionView,也可以用于UIScroll ...

  4. STL --- UVA 123 Searching Quickly

    UVA - 123 Searching Quickly Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.acti ...

  5. net 数据库连接详解 相当经典啊

    ADO.NET与抽水的故事 ADO.NET是微软新一代.NET数据库的访问架构,ADO是ActiveX Data Objects的缩写.ADO.NET是数据库应用程序和数据源之间沟通的桥梁,主要提供一 ...

  6. scons学习

    http://blog.csdn.net/andyelvis/article/category/948141 http://www.scons.org/doc/production/HTML/scon ...

  7. Javascript中void操作符

    Javascript中void是一个操作符,该操作符指定要计算一个表达式但是不返回值. void操作符用法格式如下:1.javascript:void (expression)2.javascript ...

  8. Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法

    Eclipse 出现Some sites could not be found.  See the error log for more detail.错误 解决方法 Some sites could ...

  9. Cookies简介和使用

    Cookie public class javax.servlet.http.Cookie  1作用(1)Cookie能使站点跟踪特定访问者的访问次数.最后访问时间和访问者进入站点的路径(2)Cook ...

  10. 【GOF23设计模式】解释器模式 & 访问者模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_解释器模式.访问者模式.数学表达式动态解析库式 1.解释器模式Interpreter  2.访问者模式Visitor