3

Possible Duplicate:
Should I use string.isEmpty() or “”.equals(string)?

I am wondering, what difference is there between isEmpty and equals("")? They would appear to be the exact same things to myself.

Community
  • 1
  • 1

4 Answers4

12

java.lang.String.isEmpty() is faster, because it simply compares the length of the string - which is stored in the String object - with zero:

1286       public boolean isEmpty() {
1287           return 0 == count;
1288       }

Using equals("") performs an actual string comparison - which is supposed to be slower, although the JVM can offset some of its cost. In most implementations it does inlude a length check as well:

854       public boolean equals(Object object) {
855           if (object == this) {
856               return true;
857           }
858           if (object instanceof String) {
859               String s = (String) object;
860               int hash = hashCode;  // Single read on hashCodes as they may change
861               int shash = s.hashCode;
862               if (count != s.count || (hash != shash && hash != 0 && shash != 0)) {
863                   return false;
864               }
865               for (int i = 0; i < count; ++i) {
866                   if (value[offset + i] != s.value[s.offset + i]) {
867                       return false;
868                   }
869               }
870               return true;
871           }
872           return false;
873       }

Note: Both snippets come from this java.lang.String implementation.

EDIT:

For long-running hot code, the JVM will work out the optimisations in equals("") and will probably come up with something close to isEmpty(). For one-shot calls, however, there could be a small performance difference. It's better to just use isEmpty() and save the JVM from working it out on its own.

In addition, isEmpty() is also more clear in its intent and, in my opinion, slightly easier to maintain in the long run.

Note that if the string object can also be null, this is probably better:

if ("".equals(string)) ...
thkala
  • 84,049
  • 23
  • 157
  • 201
7

The outcome is the same, but prefer isEmpty():

  • equals("") gets a different object (from the string pool, so it is not instantiation), and then verifies whether the given string is equal to that string
  • isEmpty() simply check whether the number of symbols in the string (stored in the private field count) is 0. Much less overhead.

If there is s possibility that the string can also be null, you can check apache commons-lang StringUtils.isEmpty(..)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • "Makes a new string" and "gets it from the string pool" are mutually contradictory. It doesn't make a new string. The "" is already there in the string pool. It is put there by the compiler. – user207421 Jan 31 '11 at 06:01
  • @EJP - I mean that a new object is obtained, by getting it from the pool. I.e. it's not a new instance. – Bozho Jan 31 '11 at 08:03
  • It's not a new anything. It's an existing object from the pool. It is neither 'made' nor 'new'. – user207421 Jan 31 '11 at 11:47
  • 1
    @EJB I believe it's clear that I'm aware how string pooling works. I've further improved the wording, that by "new" I mean "another" or "different from the current". – Bozho Jan 31 '11 at 11:50
  • @Bozho: That isn't clear at all from what you had written so far. 'new' simply does not mean 'different'. And "" is just a reference to an entry in the constant pool, just like any other string literal. And it's only 'different from the current' if current isn't "", which is one of the points at issue in calling the method at all. Your answer would make more sense if you just said 'equals("") compares the current string to "" by comparing first the lengths, then if the lengths are equal, compares the characters, which in this case degenerates to an empty loop. – user207421 Feb 01 '11 at 03:47
  • 1
    @EJB well, the way the answer is phrased now does not include the word 'new' at all. And my point is that the logic is more complex - it requires comparison to a (possibly) different string. Don't be pedantic. – Bozho Feb 01 '11 at 07:21
1

Functionally they're the same (by which I mean they will yield the same result). isEmpty() is more readable though in my opinion. It's more apparent when you're reading over code. It's also more efficient.

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
0

The OpenJDK source uses this implementation of isEmpty:

public boolean isEmpty() {
    return count == 0;
}

Which suggests they would return identical results. Note that equals("") creates a new string object, so it will be significantly slower than the above simple test.

moinudin
  • 134,091
  • 45
  • 190
  • 216