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.

2
  • 1
    just beautifying your code... look at the source, isEmpty() uses string.length == 0... Commented Jul 18, 2013 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. Commented Jul 18, 2013 at 17:52

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I agree this is better, though unfortunately it's less pretty (and not quite as clear).
There is something to be said for self-documenting code. I'm not sure I agree @snakedoc .
"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.
if i'm also checking for null in the same if statement, then the NPE shouldn't matter... ie: if (string == null || string.isEmpty()) { ... };
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.

7 Comments

Careful, that's java 6-. Java 7 uses value.length == 0 as there is no more sharing of char[].
so basically isEmpty() is faster if you have a significant number of checks to process, otherwise the performance hit is unlikely to make anyone cry... right?
@RohitJain Which version of java are you looking at?
@SnakeDoc. Yeah, if only the number of comparison is too much, is it going to affect. That is also not by much difference.
@SotiriosDelimanolis. Oh! I'll also check onto the Oracle's one, when I switch on my Windows PC.
|
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.

Comments

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.

Comments

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";

1 Comment

Yea but this question is tagged Java.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.