Why is there no String.Empty in Java?

前端 未结 12 1451
悲哀的现实
悲哀的现实 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条回答
  •  孤城傲影
    2020-12-07 15:59

    If you want to compare with empty string without worrying about null values you can do the following.

    if ("".equals(text))
    

    Ultimately you should do what what you believe is clearest. Most programmers assume "" means empty string, not a string someone forgot to put anything into.

    If you think there is a performance advantage, you should test it. If you don't think its worth testing for yourself, its a good indication it really isn't worth it.

    It sounds like to you try to solve a problem which was solved when the language was designed more than 15 years ago.

提交回复
热议问题