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. How can I learn to program?

    黑客与画家:硅谷创业之父paul graham关于回答‘How can I learn to program’ How can I learn to program? Find a friend wh ...

  2. CSS魔法堂:盒子模型简介

    本文讨论的是块级盒子(Block-level box)的盒子模型(Box Model) 一.W3C标准的盒子模型   二.IE盒子模型 三.两种模型的区别 W3C标准盒子模型: 外盒模型 元素空间宽度 ...

  3. sprint3 总结

    sprint3 本次的主要任务是找项目中的bug,并与客户不断地沟通以满足客户的要求.队友主要负责找项目中的bug或提出一些建议.我主要是负责与客户沟通和修复bug.总的来说进展还算顺利. 团队贡献分 ...

  4. SQL Server 多条记录的某个字段拼接

    USE [FM_Dev] GO /****** 对象: UserDefinedFunction [dbo].[GetClassNameByStudentCode] 脚本日期: 05/23/2014 1 ...

  5. 解决IIS Web部署 svg/woff/woff2字体 404错误

    最近在IIS上部署web项目的时候,发现浏览器总是报找不到woff.woff2字体的错误.导致浏览器加载字体报404错误,白白消耗了几百毫秒的加载时间. 一开始以为是路径的问题,检查发现路径也没错. ...

  6. C# on Visual Studio Code

    installation Download .NET Core SDK installer and install it. https://www.microsoft.com/net/download ...

  7. oauth授权协议的原理

    http://oauth.net/2/ 协议的原文.原来是1.0版本,现在是2.0版本了 https://ruby-china.org/topics/15396 https://blog.yorkxi ...

  8. Jsoup实现java模拟登陆

    Jsoup实现java模拟登陆 2013-10-29 14:52:05|  分类: web开发|举报|字号 订阅     下载LOFTER我的照片书  |     1:如何获取cookies. 1.1 ...

  9. Python tools for Visual Studio插件介绍

          Python tools for Visual Studio是一个免费开源的VisualStudio的插件,支持 VisualStudio 2010,2012与2013.我们想要实现的是: ...

  10. 「C语言」int main还是void main?

    从大一入学刚接触C到现在已满7个月了,虽然刚开始就知道```int main```才是标准的写法,但一直没有深刻理解为什么不能用```void main```而必须使用```int main```. ...