1

Isn't String.isBlank() equivalent to String.isEmpty() + *whitespace only check*? While looking at the source code for String.isBlank(), it would return true when the length is zero. But I would like to know if there are any scenarios in which isEmpty() would return a true and isBlank() returns a false for the same input.

Gautham M
  • 4,816
  • 3
  • 15
  • 37
  • Does [this](https://stackoverflow.com/questions/23419087/stringutils-isblank-vs-string-isempty) answer your question? – JustAnotherDeveloper Sep 10 '20 at 14:43
  • 6
    These border cases are well-explained in the docs. Have you read them? It sounds logical to me that it cannot happen that `isEmpty()` returns `true` and `isBlank()` returns `false` for the same input. But, if you have any doubts, check the docs to see if there is an exception – fps Sep 10 '20 at 14:43
  • 3
    @JustAnotherDeveloper That question is about Commons Lang `StringUtils.isBlank`, not `String.isBlank()`. – Paul Sep 10 '20 at 14:44
  • You can look this entry. https://stackoverflow.com/a/23419120/7927573 – Oguzhan Cevik Sep 10 '20 at 14:44
  • 2
    @Paul Some of the answers reference `String.isBlank()`. Among them, the accepted answer. – JustAnotherDeveloper Sep 10 '20 at 14:46
  • Does this answer your question? [StringUtils.isBlank() vs String.isEmpty()](https://stackoverflow.com/questions/23419087/stringutils-isblank-vs-string-isempty) – Modus Tollens Sep 10 '20 at 15:01

2 Answers2

5

The docs say for isEmpty():

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

The docs of isBlank():

Returns true if the string is empty or contains only white space codepoints, otherwise false.

Emphasis mine.

So isBlank() seems to cover also all cases of isEmpty().

There exists only one string where isEmpty() returns true, and that is an empty string "". isBlank() returns true if an empty string is provided as parameter.

So: no. No case exists where isEmpty() returns true and isBlank() returns false.

And if it does, it is a software bug, provided that the documentation describes the intended behaviour. Or else you have to ask Schrödinger.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
2

There is at least one input for which isEmpty() and isBlank() produce opposite results, and that's a space:

public class MyClass {
    public static void main(String args[]) {
      String s = " ";
      System.out.println("isBlank: "+s.isBlank());
      System.out.println("isEmpty: "+s.isEmpty());
    }
}

Output:

isBlank: true

isEmpty: false

The opposite case where isBlank()returns false and isEmpty returns true does not happen, because as per the documentation, isBlank() returns true only if the string is empty or contains only white space codepoints, and isEmpty() returns true if, and only if, length() is 0. And there is no case of a string of length 0 that is not considered blank.

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24
  • 1
    Op seems to know that, but asks for the other way round. – Seelenvirtuose Sep 10 '20 at 14:55
  • 1
    @Seelenvirtuose Good catch, I have edited my answer to make it more complete, with links to documentation, and actually answer what OP is asking instead of the more general "when do both methods give different output for the same input". – JustAnotherDeveloper Sep 10 '20 at 15:08