Judge that the string is not empty in Java
1. Methods to judge whether a string 'str' is not null include:
1. Str! = NULL;
2. "". Equals (STR );
3. Str. Length ()! = 0;
(Note: length is an attribute. Generally, a collection class object has an attribute to obtain the set size.
For example, array. Length indicates the length of the array.
Length () is a method. Generally, this method is used for string objects to obtain the length of a string.
Example: String. Length ();
)
Note:
1. null indicates that the string does not point to anything. If you call its method at this time, a null pointer exception will occur.
2. "" indicates that it points to a string with a length of 0, and the method to call it is safe.
3. null is not an object. "" is an object. Therefore, null does not allocate space. "" space is allocated. For example:
String str1 = NULL; STR reference is empty
String str2 = ""; STR apply an empty string
Str1 is not an instantiated object, and str2 has been instantiated.
Objects are compared with equals, and null objects are compared with equal signs.
If str1 = NULL; the following statement is incorrect:
If (str1.equals ("") | str1 = NULL ){}
The correct syntax is if (str1 = NULL | str1.equals ("") {// first judge whether it is an object. If yes, then judge whether it is a null string}
4. Therefore, to judge whether a string is null, first ensure that it is not null and then determine its length.
String STR = xxx;
If (STR! = NULL & Str. Length ()! = 0 ){}
II, Java efficiency in determining whether a string is null
(Transferred from the Internet)
Function 1: This method is intuitive and convenient, but inefficient.
Function 2: Compares the string length and high efficiency. It is the best method I know.
Function 3: the method provided by Java SE 6.0. The efficiency is almost the same as that provided by method 2. However, it is not recommended for compatibility reasons.
The following are the running results of the three methods on the machine: (the machine performance is different, for reference only)
Function 1 Use Time: 141 Ms
Function 2 Use Time: 46 Ms
Function 3 Use Time: 47 Ms
Three methodsCodeAs follows:
Method 1:
Public void function1 (string S, int N) {long starttime = system. currenttimemillis (); For (long I = 0; I <n; I ++) {If (S = NULL | S. equals ("");} Long endtime = system. currenttimemillis (); system. out. println ("function 1 use time:" + (endtime-starttime) + "Ms ");}
Method 2:
Public void function2 (string STR, int N) {long starttime = system. currenttimemillis (); For (long I = 0; I <n; I ++) {If (S = NULL | S. length () <= 0);} Long endtime = system. currenttimemillis (); system. out. println ("function 2 use time:" + (endtime-starttime) + "Ms ");}
Method 3:
Public void function3 (string STR, int N) {long starttime = system. currenttimemillis (); For (long I = 0; I <n; I ++) {If (S = NULL | S. isempty ();} Long endtime = system. currenttimemillis (); system. out. println ("function 3 use time:" + (endtime-starttime) + "Ms ");}