StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.
From the linked documentation:
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false
For comparison StringUtils.isEmpty:
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
Warning: In java.lang.String.isBlank() and java.lang.String.isEmpty() work the same except they don't return true for null.
java.lang.String.isBlank() (since Java 11)
java.lang.String.isEmpty()
StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.
From the linked documentation:
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false
For comparison StringUtils.isEmpty:
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
Warning: In java.lang.String.isBlank() and java.lang.String.isEmpty() work the same except they don't return true for null.
java.lang.String.isBlank() (since Java 11)
java.lang.String.isEmpty()
The accepted answer from @arshajii is totally correct. However just being more explicit by saying below,
StringUtils.isBlank()
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
StringUtils.isEmpty
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
In Java, should I use (String ) isEmpty() or isBlank() when checking an XML object for an empty value?
apex - Which is Better/Faster isEmpty() or isBlank()? - Salesforce Stack Exchange
string - Difference between isEmpty() and isBlank() Method in java 11 - Stack Overflow
Replace usage of deprecated StringUtils.isEmpty with !StringUtils.hasLength
Videos
I have some config file and it is deployed in many regions. Some regions have certain requirements for example lets say one region needed a username and password (basic auth). The current setup is that each deployment has it's own config file and only the instances where it is needed are these values included in the respective config.xml file.
So I have in my Java code some logic that says
'if this element is in the XML, set the corresponding variable with its value, if not do nothing'
So when I'm trying to see if that value is present or if the tag is empty/blank, which String utility should I use to check?
example code (default option left open as empty string):
if (my_config.getString(USER_ID_ELEMENT, "") != null
& my_config.getString(PASSWORD_ELEMENT, "") != null) {
my_userId = m_config.getString(USER_ID_ELEMENT, "");
my_password = m_config.getString(PASSWORD_ELEMENT, "");Here I'm checking if it is not null, but I don't think it's a very good test since I don't know if an empty XML tag is blank or is empty (i think null isn't even an option)?
String.isEmpty is marginally faster (~0.00219ms vs ~0.00206ms, about 6%). This is such a trivially small amount that there's no reason to worry about which one you use from a performance perspective.
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.
Faster here is very broad to classify, as you will need lot of considerations to test that out. It depends on what is your use case and accordingly which method fits in best.
String.isBlank handles any string even with white space, whereas the same is not true for String.isEmpty. So if you expect empty string ('') always, then using either of them should be fine.
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();
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