Is StringUtils.EMPTY recommended?

后端 未结 11 1952
情深已故
情深已故 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 02:15

    I will add my two cents here because I don't see anybody talking about String interning and Class initialization:

    • All String literals in Java sources are interned, making any "" and StringUtils.EMPTY the same object
    • Using StringUtils.EMPTY can initialize StringUtils class, as it accesses its static member EMPTY only if it is not declared final (the JLS is specific on that point). However, org.apache.commons.lang3.StringUtils.EMPTY is final, so it won't initialize the class.

    See a related answer on String interning and on Class initialization, referring to the JLS 12.4.1.

提交回复
热议问题