Why is there no String.Empty in Java?

前端 未结 12 1404
悲哀的现实
悲哀的现实 2020-12-07 15:11

I understand that every time I type the string literal \"\", the same String object is referenced in the string pool.

But why doesn\'t the String API in

12条回答
  •  -上瘾入骨i
    2020-12-07 15:42

    Apache StringUtils addresses this problem too.

    Failings of the other options:

    • isEmpty() - not null safe. If the string is null, throws an NPE
    • length() == 0 - again not null safe. Also does not take into account whitespace strings.
    • Comparison to EMPTY constant - May not be null safe. Whitespace problem

    Granted StringUtils is another library to drag around, but it works very well and saves loads of time and hassle checking for nulls or gracefully handling NPEs.

提交回复
热议问题