Is making an empty string constant worth it?

前端 未结 18 798
無奈伤痛
無奈伤痛 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:31

    We just do the following for situations like this:

    public class StaticUtils
    {
        public static boolean empty(CharSequence cs)
        {
            return cs == null || cs.length() == 0;
        }
    
        public static boolean has(CharSequence cs)
        {
            return !empty(cs);
        }
    }
    

    Then just import static StaticUtils.*

提交回复
热议问题