The method to determine if a string str is not empty is:
1, str = = NULL;
2, "". Equals (str);
3, str.length <= 0;
4, Str.isempty ();
Note: Length is the property that the generic collection class object owns and gets the size of the collection.
For example: arrays. Length is how long the array is obtained.
Length () is a method in which a generic string class object has the method and also gets the string length.
For example: String. Length ();
String str = NULL indicates that the string does not point to anything, and if this time calls his method, a null pointer exception will appear
String str = "" means that it only wants a string of length 0, when calling his method is safe
Null is not an object, and "" is an object, so null is not allocated space, "" is allocated space
String str1 = null; The str reference is empty
String str2 = ""; str references an empty string
STR1 is not an instantiated object, STR2 has been instantiated
objects are compared with equals, and null is compared by an equal sign.
If str1=null, the following syntax error:
if (Str1.equals ("") | | Str1==null) {}
The correct wording is if (str1==null| | Str1.equals ("")) {//So when judging whether a string is empty, first determine if it is not an object, and if so, then determine if it is an empty string}
So, to determine whether a string is empty, first make sure that he is not null, and then judge his length.
String str = XXX;
if (str! = null && str.length ()! = 0) {}
The first method is the least efficient, is also a method used by many people, intuitive and convenient
The second method compares the length of the string to a high efficiency and is the best method
The third method is that Java SE 6.0 only begins to provide methods that are nearly equal in efficiency and method two, but for compatibility, it is recommended to use the method two
The fourth method is a more intuitive and simple method, and the law is also very high, and two or three efficiency is similar to
Java determines that a string str is NOT null: method and Time efficiency