ชื่อโดยทั่วไปบอกว่ามันทั้งหมด ฉันมักจะทดสอบสิ่งนี้ควบคู่กับ a string == 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+ เท่านั้น