I am working in a Java project and my code shows weird behavior. Here is my code:
String access = String.valueOf(getStringvalue());
Boolean isBlank = StringUtils.isBlank(access);
In the above code, 'access' object can have null values. And it is said that if we pass a null value to StringUtils.isBlank()
, it will return true. But here I returned only false value when access
is null. What is the reason for this behavior?
I also had this problem and found the trick just after saw the source code of String.valueof(). The source code of String.valueof() is below.
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
Therefore when you pass a null object, you will get a 'null' String. Therefore your StringUtils.isBlank() will treat is as a String rather than a null object and you will get a 'false' booean value.
Hope it helps.!
I tested those code and they give the same result.
Boolean isBlank = StringUtils.isBlank("");
System.out.println(isBlank);// give true
and
Boolean isBlank = StringUtils.isBlank(null);
System.out.println(isBlank);// give true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.