first of all, I do not see "S==nul" L in this case as "s is an empty string".
first, the error that the beginner is apt to appear
1, use if (s== "") {} to determine whether S is empty.
This is the lowest error and the most covert error, saying it's low-level because the programmer who constructs the statement has little knowledge of string, and says it's hidden because not only does the statement not compile incorrectly, but many beginners think it's completely correct, but there are serious bugs. Let's look at the following example:
Example One:
public class testempty{
public static void Main (String args[]) {
String s= "";
String S=new string ("");
if (s== "") {
System.out.println ("Empty");
}
}
}
parsing : The program is run two times, the first time S is defined with string s= "", and the second s is defined by string S=new string (""), and you will find that the first execution will output empty, and the second execution has no output empty. This is where the bugs of the program are. If you know more about string, must know the cause of it; for the convenience of reading I am here to briefly explain why: in the first definition, S points to "" in the string pool, and s in the IF statement is also compared to "" in the string pool, so s== "" Returns true; in the second definition, S points to "" in the heap, but s at this point in the IF statement is still compared to the sting pool, so s== "" returns false.
2. Directly use if (S.equals ("")) {} or if (S.length () <=0) {} or if (S.isempty ()) {} to determine
This is the principle of the correct, but the consideration is not very comprehensive, the program is a bug, this is a common mistake programmers make, see the following example:
Example Two:
public class testempty{
public static void Main (String args[]) {
String S=null;
if (S.equals ("")) {
System.out.println ("Empty");
}
/*if (S.length () <=0) {
System.out.println ("Empty");
}
if (S.isempty ()) {
System.out.println ("Empty");
}*/
}
}
parsing : Run the program three times, only one of the three if statements valid each time, you will find three times will throw NullPointerException exception, because the program s==null, it points to the indeterminate object, Unable to invoke a method of a determined sting object. So our judgment statement should first determine whether S is null, so it should be the form of if (S!=null&&s.equals ("")), the other two are the same.
Ii. Efficiency Issues
In example two, there are three ways to determine if s is empty, so which is faster and more efficient. Consider the following example, which references the code of a particular guy:
Example Three:
public class testempty{
String s = "";
Long n = 10000000;
private void Function1 () {
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");
}
private void function2 () {
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");
}
private void Function3 () {
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");
}
public static void Main (string[] args) {
Testempty te=new testempty ();
Te.function1 ();
Te.function2 ();
Te.function3 ();
}
}
parsing : Multiple runs of the program several times, you will find that function2 () and Function3 () The running time is basically the same, but Function1 () running time is basically "F2" and "F3" Twice times, thus equals () The efficiency of the method is the lowest, and the efficiency of the other two methods is basically the same. Equals () efficiency is due to the internal loop of the method, the reader can look at the Java source code of the Sting class, Compare equals (), Length () and IsEmpty () method, which is limited to the length of the list.