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. sed之打印特定行与连续行

    接分号,表示打印特定行,如下命令只会打印出第一行与第十行 sed -n '1p;10p'  test.txt 接逗号,表达连续的行娄,如下命令打印出第一行到第十行 sed -n '1,10p' tes ...

  2. [Solution] ASP.NET Identity(2) 空的项目使用

    在本节中,我将说明将ASP.NET Identity添加到现有的项目或者一个空项目.我将介绍你需要添加的Nuget和Class.此示例中,会使用LocalDB. 本节目录: 注册用户 登入登出 注册用 ...

  3. sprint5.0

    团队成员完成自己认领的任务. 燃尽图:理解.设计并画出本次Sprint的燃尽图的理想线.参考图6. 每日立会更新任务板上任务完成情况.燃尽图的实际线,分析项目进度是否在正轨.每天的例会结束后的都为任务 ...

  4. XSS 和 CSRF 攻击

    web安全中有很多种攻击手段,除了SQL注入外,比较常见的还有 XSS 和 CSRF等 一.XSS(Cross Site Scripting)跨站脚本 XSS其实就是Html的注入问题,攻击者的输入没 ...

  5. c# 编程语言 编译器 Roslyn

    4 月3日,微软向公众发布了Roslyn编译器项目,该项目采用了Apache开源许可协议.C#的创始人 Anders Hejlsberg在Build大会的第二场主题演讲中将这一令人震惊的消息公之于众. ...

  6. .net xml 增删改查基础复习及干货分享

    今天做做项目时,有一个需求需要用到一些固定的文本数据,觉得将这些需要存储的信息直接写在代码里很不友好,放在数据库中存储又觉得不够方便,自然就想到了使用xml来进行操作,我平常在项目中其实用到xml的机 ...

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

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

  8. java注释指导手册

    译文出处: Toien Liu   原文出处:Dani Buiza 编者的话:注解是java的一个主要特性且每个java开发者都应该知道如何使用它. 我们已经在Java Code Geeks提供了丰富 ...

  9. R语言-神经网络包RSNNS

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...

  10. 如何rename sqlserver database

    Problem Sometimes there is a need to change the name of your database whether this is because the or ...