Test the efficiency of str. isempty () and "". Equals (STR ).
Comparison times: 999999999
Str. isempty () 2735 (2.7 seconds)
"". Equals (STR) takes 10516 (10 seconds)
The difference is so big
Public class test4 {public static void main (string [] ARGs) {long times = 999999999; string STR = ""; long a1 = system. currenttimemillis (); For (long I = 0; I <times & Str. isempty (); I ++) {// system. out. println (Str. isempty ();} Long a2 = system. currenttimemillis (); system. out. println ("str. isempty () times: "+ (A2-A1); long b1 = system. currenttimemillis (); For (long I = 0; I <times &&"". equals (STR); I ++) {// system. out. println ("". equals (STR);} Long b2 = system. currenttimemillis (); system. out. println ("\"\". equals (STR) times: "+ (B2-B1 ));}}
Output result:
Str. isempty () times: 2735 "". Equals (STR) times: 10516
In fact, looking at the source code will soon find the problem
Public Boolean isempty () {return COUNT = 0 ;}
Public Boolean equals (Object anobject) {If (this = anobject) {return true;} If (anobject instanceof string) {string anotherstring = (string) anobject; int n = count; if (n = anotherstring. count) {char V1 [] = value; char V2 [] = anotherstring. value; int I = offset; Int J = anotherstring. offset; while (n --! = 0) {If (V1 [I ++]! = V2 [J ++]) return false;} return true ;}} return false ;}