Java 我应该使用 string.isEmpty() 还是 "".equals(string)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3321526/
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
Should I use string.isEmpty() or "".equals(string)?
提问by Matt Ball
The title basically says it all. I'm usually testing this alongside a string == null
, so I'm not really concerned about a null-safe test. Which should I use?
标题基本上说明了一切。我通常将它与 a 一起测试string == null
,所以我并不真正关心空安全测试。我应该使用哪个?
String s = /* whatever */;
...
if (s == null || "".equals(s))
{
// handle some edge case here
}
or
或者
if (s == null || s.isEmpty())
{
// handle some edge case here
}
On that note - does isEmpty()
even do anything other than return this.equals("");
or return this.length() == 0;
?
在这一点上 -isEmpty()
甚至做除了return this.equals("");
或之外的任何事情return this.length() == 0;
?
采纳答案by Michael Mrozek
The main benefit of "".equals(s)
is you don't needthe null check (equals
will check its argument and return false
if it's null), which you seem to not care about. If you're not worried about s
being null (or are otherwise checking for it), I would definitely use s.isEmpty()
; it shows exactly what you're checking, you care whether or not s
is empty, not whether it equals the empty string
的主要好处"".equals(s)
是您不需要空检查(equals
将检查其参数并返回false
是否为空),您似乎并不关心。如果您不担心s
为空(或正在检查它),我肯定会使用s.isEmpty()
; 它准确地显示了您正在检查的内容,您关心是否s
为空,而不是它是否等于空字符串
回答by Kylar
It doesn't really matter. "".equals(str)
is more clear in my opinion.
这并不重要。"".equals(str)
在我看来更清楚。
isEmpty()
returns count == 0
;
isEmpty()
返回count == 0
;
回答by Fabian Steeg
One thing you might want to consider besides the other issues mentioned is that isEmpty()
was introduced in 1.6, so if you use it you won't be able to run the code on Java 1.5 or below.
除了提到的其他问题之外,您可能还想考虑的一件事是isEmpty()
1.6 中引入的,因此如果您使用它,您将无法在 Java 1.5 或更低版本上运行代码。
回答by David Young
String.equals("")
is actually a bit slower than just an isEmpty()
call. Strings store a count variable initialized in the constructor, since Strings are immutable.
String.equals("")
实际上比只是isEmpty()
打电话要慢一点。字符串存储在构造函数中初始化的计数变量,因为字符串是不可变的。
isEmpty()
compares the count variable to 0, while equals will check the type, string length, and then iterate over the string for comparison if the sizes match.
isEmpty()
将 count 变量与 0 进行比较,而 equals 将检查类型、字符串长度,如果大小匹配,则遍历字符串进行比较。
So to answer your question, isEmpty()
will actually do a lot less! and that's a good thing.
所以回答你的问题,isEmpty()
其实会少很多!这是一件好事。
回答by CoolBeans
You can use apache commons StringUtilsisEmpty() or isNotEmpty().
您可以使用 apache commons StringUtilsisEmpty() 或 isNotEmpty()。
回答by conapart3
I wrote a tester class which can test the performance:
我写了一个可以测试性能的测试器类:
public class Tester
{
public static void main(String[] args)
{
String text = "";
int loopCount = 10000000;
long startTime, endTime, duration1, duration2;
startTime = System.nanoTime();
for (int i = 0; i < loopCount; i++) {
text.equals("");
}
endTime = System.nanoTime();
duration1 = endTime - startTime;
System.out.println(".equals(\"\") duration " +": \t" + duration1);
startTime = System.nanoTime();
for (int i = 0; i < loopCount; i++) {
text.isEmpty();
}
endTime = System.nanoTime();
duration2 = endTime - startTime;
System.out.println(".isEmpty() duration "+": \t\t" + duration2);
System.out.println("isEmpty() to equals(\"\") ratio: " + ((float)duration2 / (float)duration1));
}
}
I found that using .isEmpty() took around half the time of .equals("").
我发现使用 .isEmpty() 花费的时间大约是 .equals("") 的一半。