Is making an empty string constant worth it?

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

    String literals are interned by default, so no matter how many times you refer to "" in code, there will only be one empty String object. I don't see any benefit in declaring EMPTY_STRING. Otherwise, you might as well declare ONE, TWO, THREE, FOUR, etc. for integer literals.

    Of course, if you want to change the value of EMPTY_STRING later, it's handy to have it in one place ;)

提交回复
热议问题