Should I Use String.Isempty() or "".Equals(String)

Should I use string.isEmpty() or .equals(string)?

The main benefit of "".equals(s) is you don't need the null check (equals will check its argument and return false if it's null), which you seem to not care about. If you're not worried about s being null (or are otherwise checking for it), I would definitely use s.isEmpty(); it shows exactly what you're checking, you care whether or not s is empty, not whether it equals the empty string

Difference between String.isEmpty() and String.equals()

I think isEmpty() is a bit more efficient. However a smart compiler may optimize the equals("") call anyway. From the OpenJDK source:

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

1013 public boolean equals(Object anObject) {
1014 if (this == anObject) {
1015 return true;
1016 }
1017 if (anObject instanceof String) {
1018 String anotherString = (String)anObject;
1019 int n = count;
1020 if (n == anotherString.count) {
1021 char v1[] = value;
1022 char v2[] = anotherString.value;
1023 int i = offset;
1024 int j = anotherString.offset;
1025 while (n-- != 0) {
1026 if (v1[i++] != v2[j++])
1027 return false;
1028 }
1029 return true;
1030 }
1031 }
1032 return false;
1033 }

Also the answer here on whether to use str.isEmpty() or "".equals(str) is spot on:

The main benefit of "".equals(s) is you don't need the null check (equals will check its argument and return false if it's null), which you seem to not care about. If you're not worried about s being null (or are otherwise checking for it), I would definitely use s.isEmpty(); it shows exactly what you're checking, you care whether or not s is empty, not whether it equals the empty string

Difference between isEmpty and equals()

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)) ...

StringUtils.isBlank() vs String.isEmpty()

StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.

From the linked documentation:

Checks if a String is whitespace, empty ("") or null.

 StringUtils.isBlank(null)      = true
StringUtils.isBlank("")        = true
StringUtils.isBlank(" ")       = true
StringUtils.isBlank("bob")     = false
StringUtils.isBlank("  bob  ") = false

For comparison StringUtils.isEmpty:

 StringUtils.isEmpty(null)      = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false

Warning: In java.lang.String.isBlank() and java.lang.String.isEmpty() work the same except they don't return true for null.

java.lang.String.isBlank() (since Java 11)

java.lang.String.isEmpty()

Any difference between String = null and String.isEmpty?

The empty string is a string with zero length. The null value is not having a string at all.

  • The expression s == null will return false if s is an empty string.
  • The second version will throw a NullPointerException if the string is null.

Here's a table showing the differences:

+-------+-----------+----------------------+
| s | s == null | s.isEmpty() |
+-------+-----------+----------------------+
| null | true | NullPointerException |
| "" | false | true |
| "foo" | false | false |
+-------+-----------+----------------------+

Can we compare a string with == to an empty value

Of course you can (insofar that compilation will pass), although you will probably not get the result you expect since using == will compare references not contents.

My favourite way is to use the Yoda Expression "".equals(myString) since then you don't need to pre-test myString for null.

Else you could use myString.isEmpty().



Related Topics



Leave a reply



Submit