Difference between isEmpty() and isBlank() Method in java 11
Asked Answered
D

5

40

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?

Domela answered 12/7, 2018 at 6:58 Comment(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.Napoli
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.Peppard
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.Nidorf
Upvoting because I just googled it and this was the first result.Blunder
Upvoting. The answer is not crystal clear, in particular for people who first language is not English.Shoulder
D
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();
Domela answered 12/7, 2018 at 6:58 Comment(3)
The "Before Java 11" and "After Java 11"-examples are not equivalent. See #51267082Ecclesiolatry
Note that a null.isEmpty() throws an NPE.Comenius
Based on @Comenius 's comment I prefer using apache.commons for such things for being able to automatically handle null values.Hoes
P
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

Plexiform answered 2/6, 2020 at 9:46 Comment(0)
L
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);
    }
}
Lasagne answered 3/5, 2019 at 14:13 Comment(1)
Your statement that isBlank() is equal to str.trim().isEmpty() is wrong. trim() does not take out unicode whitespace characters. strip() and isBlank() do.Thomism
M
1

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.

Mezzosoprano answered 9/1, 2023 at 8:37 Comment(1)
You know what, I used isBlank() on null string which resulted into NPE.Shoa
A
0

From https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html

enter image description here

In case you can't see the screenshot:

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

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

Appointive answered 13/12, 2023 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.