Is making an empty string constant worth it?

前端 未结 18 718
無奈伤痛
無奈伤痛 2021-01-01 10:11

I have a co-worker that swears by

//in a singleton \"Constants\" class
public static final String EMPTY_STRING = \"\";

in a constants class

18条回答
  •  伪装坚强ぢ
    2021-01-01 10:35

    As a tangent to the question, I generally recommend using a utility function when what you're really checking for is "no useful value" rather than, specifically, the empty string. In general, I tend to use:

    import org.apache.commons.lang.StringUtils;
    
    // Check if a String is whitespace, empty ("") or null.
    StringUtils.isBlank(mystr); 
    // Check if a String is empty ("") or null.
    StringUtils.isEmpty(mystr); 
    

    The concept being that the above two:

    • Check the various other cases, including being null safe, and (more importantly)
    • Conveys what you are trying to test, rather than how to test it.

提交回复
热议问题