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?
Practically speaking, you should generally use String. isBlank if you expect potentially whitespace strings (e.g. from user input that may be all whitespace), and String. isEmpty when checking fields from the database, which will never contain a string of just whitespace.
isBlank() is an instance method that returns true if the string is empty or contains only white space codepoints. This method was introduced in Java 11. If the string contains only white spaces, then applying this method will return true .
StringUtils isEmpty() Example in Java.The isEmpty() method doesn't check for whitespace. It will return false if our string is whitespace. We need to use StringUtils isBlank() method to check whitespace.
In Java 11, a new method called isBlank() was added in the String class. The isBlank() method will return true in the below cases: If the string is empty. If the string only contains whitespaces.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With