Why is there no String.Empty in Java?

前端 未结 12 1426
悲哀的现实
悲哀的现实 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 16:02

    If you really want a String.EMPTY constant, you can create an utility static final class named "Constants" (for example) in your project. This class will maintain your constants, including the empty String...

    In the same idea, you can create ZERO, ONE int constants... that don't exist in the Integer class, but like I commented, it would be a pain to write and to read :

    for(int i=Constants.ZERO; ...) {
        if(myArray.length > Constants.ONE) {
            System.out.println("More than one element");
        }
    }
    

    Etc.

提交回复
热议问题