Is StringUtils.EMPTY recommended?

后端 未结 11 1929
情深已故
情深已故 2020-12-08 01:46

Do you use StringUtils.EMPTY instead of \"\"?

I mean either as a return value or if you set a the value of a String variable. I don\'t mean

11条回答
  •  春和景丽
    2020-12-08 02:05

    I find StringUtils.EMPTY useful in some cases for legibility. Particularly with:

    1. Ternary operator eg.

      item.getId() != null ? item.getId() : StringUtils.EMPTY;
      
    2. Returning empty String from a method, to confirm that yes I really wanted to do that.

    Also by using a constant, a reference to StringUtils.EMPTY is created. Otherwise if you try to instantiate the String literal "" each time the JVM will have to check if it exists in the String pool already (which it likely will, so no extra instance creation overhead). Surely using StringUtils.EMPTY avoids the need to check the String pool?

提交回复
热议问题