General Programming

Rule 6: Avoid strings where other types are more appropriate

Following are a few situations where we should not use the String as replacement:
1.Strings are poor substitutes for other value types.
when we read from file/network/keyboard all are string type. if the data being read is of int or float or boolean type it should be kept in the same type.
2.Strings are poor substitutes for enum types.- use enums for constants
3.Strings are poor substitutes for aggregate types.
// Inappropriate use of string as aggregate type
String compoundKey = className + “#” + i.next();
since we need to parse the string in case we want to access particular data.
4.Strings are poor substitutes for capabilities.
for naming ThreadLocal() .. we had earlier(before v1.2) the method like :
// 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);
}

which is insecure and error prone . suppose two different client using this with same name both will fail.
A malicious user can acces by provind the name

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment