2

I've read through this SO thread: Java, check whether a string is not null and not empty?

The question that arises (and was not answered in that thread), is:

Why is string.isEmpty() better than using string.equals("") ? In the same answer the poster states that prior to J6, people would use string.length == 0 as a way to check for empty strings. Am I missing something, because they all seem to do exactly the same thing... making it just a matter of beautifying your code.

Community
  • 1
  • 1
SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
  • 1
    just beautifying your code... look at the source, `isEmpty()` uses `string.length == 0`... – Lucas Jul 18 '13 at 17:52
  • 1
    Probably speed of execution. The problem is that each new browser version makes this or that feature take less of more time to execute so the conversation gets a different answer depending on "when" it happened. – Lee Meador Jul 18 '13 at 17:52

6 Answers6

6

The best to use is

"".equals(yourString) 

as this avoids the null pointer exception.

If you use

string.equals("") 

and if your string is null, it will throw a null pointer exception.

and again the problem with isEmpty is that

It Returns true if, and only if, length() is 0

So if your string is empty it will also cause NPE.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • I agree this is better, though unfortunately it's less pretty (and not quite as clear). – Reinstate Monica -- notmaynard Jul 18 '13 at 17:55
  • 1
    There is something to be said for self-documenting code. I'm not sure I agree @snakedoc . – BlackVegetable Jul 18 '13 at 17:58
  • 3
    "So if your string is empty it will also cause NPE." -- If your string is empty, isEmpty() will return true. If your *reference* of String type is null, attempting to call isEmpty() will throw a NPE, as would attempting to call any other method. – Andy Thomas Jul 18 '13 at 17:59
  • if i'm also checking for `null` in the same `if` statement, then the NPE shouldn't matter... ie: `if (string == null || string.isEmpty()) { ... };` – SnakeDoc Jul 18 '13 at 18:03
4

Why is string.isEmpty() better than using string.equals("") ?

Just look at the source of String#isEmpty():

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

It simply returns the result of comparing count variable stored as a part of the class with 0. No heavy computation. The value is already there.

Whereas, the String.equals() method does lot of computations, viz, typecasting, reference comparison, length comparison - String#equals(Object) source code. So, to avoid those runtime operations, you can simply use isEmpty() to check for empty strings. That would be a slower by a minute difference though.


Note: The Oracle Java 7 version of String.isEmpty() uses value.length == 0, as it no more stores the count and offset variable. In openjdk-7 though, it still uses the count variable there.

But still, the value.length computation will be a bit faster than all those operations in equals() method.
Apart from the performance difference, which is really not of much concern, and the difference if any would be minute, the String.isEmpty() method seems more clear about your intents. So, I prefer to use that method for checking for empty strings.

And at last, of course, don't believe on what you see. Just benchmark your code using both the methods, and see for any measurable differences if any.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

The call of isEmpty() expresses your intentions better, thus improving readability. Readers spend less time understanding what is your intention behind the check: rather than thinking that you are interested in checking string's equality or determining its length, they see that all you want to know is if the string is empty or not.

There is no performance difference, as isEmpty() is implemented by checking the length.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

as Lucas stated, one's not better than the other...isEmpty is a little more readable, nothing more, nothing less. There's no significant performance hit or anything like that using one over the other.

user2366842
  • 1,231
  • 14
  • 23
0

There are many ways to get one answer, everything is depending on the programming language and how to program your.

For Example in C#:

 if (String.IsNullOrEmpty(s)) 
    return "is null or empty";
0

Use the util method provided by apache commons

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isEmpty%28java.lang.String%29

If you feel like you can even write your own isEmpty(String input) method

This approach has following advantages It makes your intentions clear You are safe in case of nulls.

Mangoose
  • 922
  • 1
  • 9
  • 17