37

Java 11 has added A new instance method isBlank() to java.lang.String class.

What's the basic difference between the existing isEmpty and newly added isBlank() method?

Naman
  • 27,789
  • 26
  • 218
  • 353
Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
  • 5
    Downvoted because the questions is somewhat arbitrary (`isempty` and `isBlank` pretty much tell you what the difference is) and easily answerable by looking at their Javadoc. – Nicolai Parlog Jul 12 '18 at 08:52
  • 1
    On the contrary to the ease of complexity. Would actually await someone to pitch in and speak about any difference(if) in the performance of the existing solution versus the `isBlank`, since the JDK link reads *"avoids any object construction..."* as well. :) Couldn't find the numbers on the link either. – Naman Jul 12 '18 at 08:53
  • 3
    Technically, the question deserves a down-vote for lack of research. But its one of those questions that are probably searched often and as long as theres no duplicate, well. So it probably is worth having it here on SO. – Zabuzard May 03 '19 at 14:15
  • 8
    Upvoting because I just googled it and this was the first result. – Bal May 15 '19 at 17:45
  • 2
    Upvoting. The answer is not crystal clear, in particular for people who first language is not English. – luis.espinal Jul 20 '21 at 23:45

4 Answers4

53

isEmpty()

The java string isEmpty() method checks if this string is empty. It returns true, if the length of the string is 0 otherwise false e.g.

System.out.println("".isEmpty()); // Prints - True
System.out.println(" ".isEmpty()); //Prints - False 

Java 11 - isBlank()

The new instance method java.lang.String.isBlank() returns true if the string is empty or contains only white space, where whitespace is defined as any codepoint that returns true when passed to Character#isWhitespace(int).

boolean blank = string.isBlank();

Before Java 11

boolean blank = string.trim().isEmpty();

After Java 11

boolean blank = string.isBlank();
Naman
  • 27,789
  • 26
  • 218
  • 353
Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
  • 14
    The **"Before Java 11"** and **"After Java 11"**-examples are not equivalent. See https://stackoverflow.com/questions/51266582/difference-between-string-trim-and-strip-methods-in-java-11 – Hulk Jul 12 '18 at 09:41
  • 4
    Note that a null.isEmpty() throws an NPE. – MarkHu May 21 '21 at 23:20
  • 1
    Based on @MarkHu 's comment I prefer using apache.commons for such things for being able to automatically handle null values. – Dirk Schumacher Jun 17 '21 at 09:31
5

The difference is as below :-

isBlank() returns true for the string having only white space characters whereas isEmpty() will return false for such strings.

("\n\r  ").isBlank();  //returns true
("\n\r  ").isEmpty();  //returns false

For detailed explanation with Code Example visit : isBlank() vs isEmpty() in String class Java

Vyom
  • 121
  • 1
  • 3
2

Java 11 added has new method called .isBlank() in String class

  1. isBlank() method is equal to str.trim().isEmpty() in earlier to java 11 versions
  2. isEmpty() : Returns true if, and only if, length() is 0

This is the internal implementation of isBlank() method in String class of java 11

public boolean isBlank() {
    return indexOfNonWhitespace() == length();
}

private int indexOfNonWhitespace() {
    if (isLatin1()) {
        return StringLatin1.indexOfNonWhitespace(value);
    } else {
        return StringUTF16.indexOfNonWhitespace(value);
    }
}
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
  • Your statement that `isBlank()` is equal to `str.trim().isEmpty()` is wrong. `trim()` does not take out unicode whitespace characters. `strip()` and `isBlank()` do. – Robert Jan 11 '22 at 18:24
0

When to use isNull, isEmpty, isBlank isNull() Checks if the value is "null".

isNull(null) = true
isNull("") = false
isNull(" ") = false
isNull("bob") = false
isNull(" bob ") = false

Returns true if the string is null.

isEmpty() Checks if the value is an empty string containing no characters or whitespace.

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

Returns true if the string is null or empty.

**

isBlank() Checks if the value is null, empty, or contains only whitespace characters.

**

*

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

Returns true if the string is null, empty, or only whitespace.