Is checking whether string.length == 0 still faster than checking string == ""?

21,725

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.

Share:
21,725
WSBT
Author by

WSBT

A confused human trying to understand computers.

Updated on July 22, 2022

Comments

  • WSBT
    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 think string == "" is more straightforward and more readable. I mostly deal with high-level languages such as .NET and java.

  • Jagger
    Jagger over 11 years
    How can it return true for a null if the isEmpty() is invoked on this null reference? I would rather expect a NullPointerException in this case.
  • ddavison
    ddavison over 11 years
    because it is considered "empty"
  • Jagger
    Jagger over 11 years
    I 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 return true?
  • Alex Feinman
    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.
    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
    Dan Puzey over 11 years
    What'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.
    Daniel Mühlbachler-P. over 11 years
    I'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
    Richard Le Mesurier over 10 years
    +1 for the Yoda code (best practice)