Is checking whether string.length == 0 still faster than checking string == ""?
Solution 1
Usually, string object store their length and therefore getting and comparing the integer is very fast and has less memory access than an equals() where you - in the worst case - have to check the length and loop over the characters.
Anyway, nowadays the equals() method of a string should also check for the length first and therefore it should be - nearly - the same speed as checking for the length.
equals part in Java (http://www.docjar.com/html/api/java/lang/String.java.html):
int n = count;
if (n == anotherString.count) {...}
equals part in Objective-C (http://www.opensource.apple.com/source/CF/CF-476.15/CFString.c) - NSString is based on CFString:
if (len1 != __CFStrLength2(str2, contents2)) return false;
Solution 2
The best way to do that test in Java is
"".equals(string)
because that handles the case where string is null.
As for which is faster, I think the answer is that it doesn't matter. Both are very fast and which one is actually fastest depends on internal compiler implementation.
Solution 3
Just use string.isEmpty()
.
(I reject "".equals(string)
because if you have a null, that probably indicates a bug that should crash the program because it needs to be fixed. I'm intolerant of nulls.)
Solution 4
You need to be careful about using ==
to test for string equality. If the variable string is not interned, there's a good chance that the test will fail.
String a = "abc";
String b = a.substring(3);
System.out.println("b == \"\": " + (b == "")); // prints false
System.out.println("b.equals(\"\"): " + b.equals("")); // prints true
I'd use string.length() == 0
or string.equals("")
. Benchmark to see which is faster.
Comments
-
WSBT almost 2 years
I read from a programming book about 7-8 years ago that checking
string.length == 0
is a faster way to check for empty strings. I'm wondering if that statement still holds true today (or if it has ever been true at all), because I personally thinkstring == ""
is more straightforward and more readable. I mostly deal with high-level languages such as .NET and java. -
Jagger over 11 yearsHow can it return
true
for anull
if theisEmpty()
is invoked on thisnull
reference? I would rather expect aNullPointerException
in this case. -
ddavison over 11 yearsbecause it is considered "empty"
-
Jagger over 11 yearsI do not think you understood the point. In your example you invoke this method on an empty string, like this
"".isEmpty()
. Are you trying to say that this invocation:String nullString = null; nullString.isEmpty();
will returntrue
? -
Alex Feinman over 11 years"usually" by what standard? For example, it's never done that way with native C strings (which are null terminated)
-
Daniel Mühlbachler-P. over 11 years"usually" means in higher-level programming languages. C is really low-level and yes, of course, there is no length information. Furthermore, the question was tagged with Java and he wrote he mostly deals with high-level programming languages which indicates that we talk about higher-level programming languages.
-
Dan Puzey over 11 yearsWhat's your evidence for this? In .NET the recommended method would be to use
string.IsNullOrEmpty()
. You say "nowadays the equals() method should check length first," but you provide no evidence that this is actually the case. Bad answer! -
Daniel Mühlbachler-P. over 11 yearsI'm a Java and Objective-C developer and here is the proof: opensource.apple.com/source/CF/CF-476.15/CFString.c (
if (len1 != __CFStrLength2(str2, contents2)) return false;
) - NSString is based on CFString; docjar.com/html/api/java/lang/String.java.html (if (n == anotherString.count) {...}
) -
Richard Le Mesurier over 10 years+1 for the Yoda code (best practice)