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?
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?
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
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();
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
Java 11 added has new method called .isBlank()
in String
class
isBlank()
method is equal to str.trim().isEmpty()
in earlier to java 11 versionsisEmpty()
: Returns true if, and only if, length() is 0This 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);
}
}
isBlank()
is equal to str.trim().isEmpty()
is wrong. trim()
does not take out unicode whitespace characters. strip()
and isBlank()
do. –
Thomism isNull(null) = true
isNull("") = false
isNull(" ") = false
isNull("bob") = false
isNull(" bob ") = false
Returns true if the string is null.
isEmpty(null) = true
isEmpty("") = true
isEmpty(" ") = false
isEmpty("bob") = false
isEmpty(" bob ") = false
Returns true if the string is null or empty.
**
**
*
isBlank(null) = true
isBlank("") = true
isBlank(" ") = true
isBlank("bob") = false
isBlank(" bob ") = false
Returns true if the string is null, empty, or only whitespace.
From https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html
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.
© 2022 - 2024 — McMap. All rights reserved.
isempty
andisBlank
pretty much tell you what the difference is) and easily answerable by looking at their Javadoc. – NapoliisBlank
, since the JDK link reads "avoids any object construction..." as well. :) Couldn't find the numbers on the link either. – Peppard