Method 1: The most popular method is intuitive and convenient, but the efficiency is very low:
if (s == null || "". equals (s));
Method 2: Compare the length of the string, high efficiency, is the best method I know:
if (s == null || s.length () <= 0);
Method 3: The method provided by Java SE 6.0 only, the efficiency is almost the same as Method 2, but for compatibility reasons, Method 2 is recommended.
if(s == null || s.isEmpty());
Method 4: This is a more intuitive and convenient method, and the efficiency is also very high, which is similar to the efficiency of methods 2 and 3:
if (s == null || s == "");
Note: s == null is necessary to exist.
If the String type is null, performing equals (String) or length () operations will throw java.lang.NullPointerException.
And the order of s == null must appear first, otherwise java.lang.NullPointerException will also be thrown.
The following Java code:
String str = null;
if (str.equals ("") || str = == null) {// will throw an exception
System.out.println("success");
}
// "" .equals (str); The postposition ensures that no error will be reported in case of null.