我通常会与string == null
一起测试,因此我并不真的关心一个安全的空值测试。那么我应该使用哪个?
我通常会和“字符串== null”一起测试,所以我不太关心安全的空值测试。我应该使用哪一个?
String s = /* whatever */;
...
if (s == null || "".equals(s))
{
// handle some edge case here
}
或者
if (s == null || s.isEmpty())
{
// handle some edge case here
}
在此提醒一下 - isEmpty()
方法是否除了 return this.equals("");
或者 return this.length() == 0;
之外还有其他功能呢?
isEmpty()
仅适用于 Java 6 及以上版本。 - ColinD