I’m usually testing this alongside a string == null, so I’m not really concerned about a null-safe test. Which should I use?
String s = /* whatever */;
...
if (s == null || "".equals(s))
{
// handle some edge case here
}
or
if (s == null || s.isEmpty())
{
// handle some edge case here
}
On that note – does isEmpty() even do anything other than return this.equals(""); or return this.length() == 0;?
The main benefit of
"".equals(s)is you don’t need the null check (equalswill check its argument and returnfalseif it’s null), which you seem to not care about. If you’re not worried aboutsbeing null (or are otherwise checking for it), I would definitely uses.isEmpty(); it shows exactly what you’re checking, you care whether or notsis empty, not whether it equals the empty string