In if-statements in Java code i often read something like if(string != null && string.isEmpty() == false). I was used to write if(string != null && !string.isEmpty())
Are there any disadvantages of using !string.isEmpty()?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In terms of behaviour
and
are obviously identical. I prefer the latter, only because it’s slightly more concise. Where possible, I prefer to express conditions positively, but unfortunately there is no
string.isNotEmpty()methodSo in general when I want to check if a String is either null or empty (in a null-safe fashion) I use:
This StringUtils class is from the Apache commons lang library, but you can easily write your own if you don’t already have this on your classpath and don’t want to add it.