Java 是否推荐使用 StringUtils.EMPTY?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4095501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:34:55  来源:igfitidea点击:

Is StringUtils.EMPTY recommended?

javastringapache-commons

提问by keuleJ

Do you use StringUtils.EMPTYinstead of ""?

你用StringUtils.EMPTY代替""吗?

I mean either as a return value or if you set a the value of a String variable. I don't mean for comparison, because there we use StringUtils.isEmpty()

我的意思是作为返回值,或者如果您设置字符串变量的值。我不是要比较,因为我们使用StringUtils.isEmpty()

采纳答案by David Pierre

Of course not. Do you really think "" is not clear enough ?

当然不是。你真的觉得“”还不够清楚吗?

Constants have essentially 3 use cases:

常量基本上有 3 个用例:

  1. Document the meaning of a value (with constant name + javadoc)
  2. Synchronize clients on a common value.
  3. Provide a shortcut to a special value to avoid some init costs
  1. 记录值的含义(使用常量名 + javadoc)
  2. 根据公共值同步客户端。
  3. 提供特殊值的快捷方式以避免一些初始化成本

None apply here.

无一适用于此。

回答by Romain Linsolas

I don't really like to use it, as return "";is shorter than return StringUtils.EMPTY.

我真的不喜欢使用它,因为return "";它比return StringUtils.EMPTY.

However, one falseadvantage of using it is that if you type return " ";instead of return "";, you may encounter different behavior (regarding if you test correctly an empty String or not).

但是,使用它的一个错误优势是,如果您键入return " ";而不是return "";,您可能会遇到不同的行为(关于您是否正确测试空字符串)。

回答by Christian Kuetbach

No, because I have more to write. And an emptyString is plattform independent empty(in Java).

不,因为我还有更多要写。而一个字符串是plattform独立(在Java中)。

File.separatoris better than "/" or "\".

File.separator比“/”或“\”更好。

But do as you like. You can't get an typo like return " ";

但是,随心所欲。你不能打错字return " ";

回答by cherouvim

If your class doesn't use anything else from commons then it'd be a pity to have this dependency just for this magic value.

如果您的班级不使用公共资源中的任何其他内容,那么仅仅为了这个神奇的价值而拥有这种依赖性将是一种遗憾。

The designer of the StringUtils makes heavy use of this constant, and it's the right thing to do, but that doesn't mean that you should use it as well.

StringUtils 的设计者大量使用了这个常量,这是正确的做法,但这并不意味着您也应该使用它。

回答by Bozho

Honestly, I don't see much use of either. If you want to compare egainst an empty string, just use StringUtils.isNotEmpty(..)

老实说,我认为两者都没有多大用处。如果你想比较一个空字符串,只需使用StringUtils.isNotEmpty(..)

回答by Christopher Klewes

I use StringUtils.EMPTY, for hiding the literal and also to express that return StringUtils.EMPTYwas fully expected and there should return an empty string, ""can lead to the assumption that ""can be easily changed into something else and that this was maybe only a mistake. I think the EMPTYis more expressive.

我使用StringUtils.EMPTY, 来隐藏文字并表达return StringUtils.EMPTY完全预期的内容,并且应该返回一个空字符串,这""可能会导致假设""可以轻松更改为其他内容,而这可能只是一个错误。我觉得这个EMPTY更具有表现力。

回答by Erick Robertson

No, just use "".

不,只需使用"".

The literal ""is clear as crystal. There is no misunderstanding as to what was meant. I wouldn't know why you would need a class constant for that. I can only assume that this constant is used throughout the package containing StringUtilsinstead of "". That doesn't mean you should use it, though.

字面""清晰如水晶。没有误解是什么意思。我不知道为什么你需要一个类常量。我只能假设这个常量在整个包中使用,StringUtils而不是"". 但这并不意味着您应该使用它。

If there's a rock on the sidewalk, you don't have to throw it.

如果人行道上有一块石头,你不必扔掉它。

回答by Tom

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

我发现StringUtils.EMPTY在某些情况下对于易读性很有用。特别是:

  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.
  1. 三元运算符例如。

    item.getId() != null ? item.getId() : StringUtils.EMPTY;
    
  2. 从方法返回空字符串,以确认是的,我真的想这样做。

Also by using a constant, a reference to StringUtils.EMPTYis 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.EMPTYavoids the need to check the String pool?

同样通过使用常量,StringUtils.EMPTY创建了对的引用。否则,如果您""每次尝试实例化字符串文字,JVM 将不得不检查它是否已经存在于字符串池中(它可能会存在,因此没有额外的实例创建开销)。当然使用StringUtils.EMPTY可以避免检查字符串池的需要吗?

回答by Matthieu

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

我将在这里添加我的两分钱,因为我没有看到有人在谈论String实习和类初始化:

  • All Stringliterals in Java sources are interned, making any""and StringUtils.EMPTYthe sameobject
  • Using StringUtils.EMPTYcaninitialize StringUtilsclass, as it accesses its static member EMPTYonly if it is not declared final(the JLS is specific on that point). However, org.apache.commons.lang3.StringUtils.EMPTYisfinal, so it won't initialize the class.
  • 所有String的Java源代码文字被拘禁,使得任何""StringUtils.EMPTY同一对象
  • 使用StringUtils.EMPTY可以初始化StringUtils类,因为它EMPTY仅在未声明的final情况下访问其静态成员(JLS 在这一点上是特定的)。但是,最终的,因此它不会初始化该类。org.apache.commons.lang3.StringUtils.EMPTY

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

请参阅有关字符串实习类初始化相关答案,参考JLS 12.4.1

回答by j__m

I'm amazed at how many people are happy to blindly assume that "" is indeed an empty string, and doesn't (accidentally?) contain any of Unicode's wonderful invisible and non-spacing characters. For the love of all that is good and decent, use EMPTY whenever you can.

我很惊讶有多少人乐于盲目地假设 "" 确实是一个空字符串,并且不(偶然?)包含任何 Unicode 美妙的不可见和非空格字符。出于对一切美好和体面的热爱,请尽可能使用 EMPTY。