Java – what are the disadvantages of writing! string. isEmpty()?
•
Java
In if statements in Java code, I often read if (string! = null & & string. Isempty() = = false) and so on I am used to writing if (string! = null & &! String. Isempty())
use! string. What are the disadvantages of isempty()?
Solution
In terms of behavior
if(string != null && string.isEmpty() == false)
and
if(string != null && !string.isEmpty())
Obviously the same I prefer the latter because it is more concise When possible, I prefer to actively express conditions, but unfortunately there is no string Isnotempty() method
So generally speaking, when I want to check whether the string is null or empty (in a null safe way), I use:
StringUtils.isNotBlank(string)
This stringutils class comes from the Apache commons Lang library, but if you don't have this on the classpath and don't want to add it, you can easily write your own class
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码