Item 50 - Avoid strings where other types are more appropriate

From Effective Java 2/e by Joshua Bloch

  • Strings are poor substitutes for other value types
    • When a piece of data comes into a program from a file, from the network, or from keyboard input, it should be translated into the appropriate type (int, float, boolean)
  • Strings are poor substitutes for enum types
  • Strings are poor substitutes for aggregate types

    // Inappropriate use of string as aggregate type
    String compoundKey = className + "#" + i.next();
    
    • To access individual fields, you have to parse the string, which is slow, tedious, and error-prone
  • Strings are poor substitutes for capabilities

    // 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);
    }
    
  • String keys represent a shared glo- bal namespace for thread-local variables. In order for the approach to work, the client-provided string keys have to be unique

    public final class ThreadLocal<T> {
      public ThreadLocal() { }
      public void set(T value);
      public T get();
    }
    

Posted by The Finest Artist