tips for determining whether a string is empty Java code //Comparison method One, most people are so much more string param = config.getinitparameter (" Log-level "); if (param != null && ! "". Equals (param)) { final level level = log.getlevel (param); if (level != null) log.lookup ( "Org.zkoss"). Setlevel (level); else log.error ("unknown log-level: "+param); } //comparison Method two, (and I study the source of the basic are so compared) string param =&Nbsp;config.getinitparameter ("Log-level"); if (param != null && param.length () > 0) { final level level = log.getlevel (param); if (level != null) log.lookup ( "Org.zkoss"). Setlevel (level); else log.error ("unknown log-level: "+param);   // below Let's take a look at Equals,length,isempty's source //string The Equals method internal code public boolean equals ( Object anobject) { if (this == anobject) { return true; } if (anobject instanceof string) &nbsP { 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;  } //string.length () source /** & nbsp * returns the length of this string. &NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;THE&NBSP;LENGTH&NBSP;IS&NBSP;EQUAL&NBSP;TO&NBSP;THE&NBSP;NUMBER&NBSP;OF <a href= "Character.html#unicode" >unicode * code units </a> in the string. * * @return the length of the sequence of characters represented by this * object. */ public int length () { return count; }
Comparison method One, most people are so compared String param = config.getinitparameter ("Log-level"); if (param!= null &&! "".
Equals (param)) {final level level = Log.getlevel (param);
if (level!= null) log.lookup ("Org.zkoss"). Setlevel (level);
else Log.error ("Unknown log-level:" +param);
//Comparison method Two (while I study the source code is basically so compared) String param = Config.getinitparameter ("Log-level");
if (param!= null && param.length () > 0) {final level level = Log.getlevel (param);
if (level!= null) log.lookup ("Org.zkoss"). Setlevel (level);
else Log.error ("Unknown log-level:" +param); //Let's take a look at the equals,length,isempty source//string the Equals method internal code public boolean equals (Object anobject) {if (this = = a
Nobject) {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;
The source/** * Returns of//string.length () is the length of this String. * The length is equal to the number of <a href= "Character.html#unicode" >unicode * Code units</a> in the S
Tring.
* * @return The length of the sequence of characters represented by this * object.
*/public int length () {return count; }
Java code //string.isempty source /** * returns <tt>true</tt> if, and only if, {@link #length ()} is <tt>0</tt>. * * @return <tt>true</tt> if {@link #length ()} is <tt>0</tt>, otherwise * <tt>false</tt> * * @since 1.6 */ public boolean IsEmpty () { return count == 0; }
String.isempty source
/**
* Returns <tt>true</tt> If, and only if, {@link #length ()} is <tt>0 </tt>.
*
* @return <tt>true</tt> if {@link #length ()} is <tt>0</tt>, otherwise
* <tt> false</tt>
*
* @since 1.6
/public boolean isempty () {return
count = 0;
}
The length,isempty is very simple in terms of code and logic, and for general comparison judgments,
First of all I guess: Length,isempty is the same, the Equals method is too,
Here's a test to prove our guess.
The test code is as follows
Java code private static final string sunflower = "Sunflower"; private static final int COUNT= 1000000000; public static void main (String[] args) { try { thread.sleep (2000); } catch (interruptedexception e) { e.printstacktrace () ; } system.out.println ("Start calculation .../n"); long begin = system.curRenttimemillis (); for (int j = 0; j < count; j++) { testemptybylength (sunflower); } calculateduration ("Length test", begin); begin = System.currenttimemillis (); for (int j = 0; j < count; j++) { testemptybyempty (sunflower); } calculateduration ("empTy test ", begin); begin = System.currenttimemillis (); for (int j = 0; j < count; j++) { testemptybyequals (sunflower); } calculateduration ("Equals test", begin); } public static Void calculateduration (string method, long begin) { system.out.println (method + "/t:" +&nBSP; (System.currenttimemillis () - begin) / 1000); } public static Boolean testemptybylength (STRING&NBSP;STR) { return str == null | | str.length () < 1;   /** The * empty method is jdk1.6 added, if the earlier version is incompatible * * @param str * @return */ public static boolean Testemptybyempty (STRING&NBSP;STR) { return str == null | | str.isempty (); &NBSP;&NBSP;&NBSP;&NBSP} public static boolean Testemptybyequals (STRING&NBSP;STR) { return "". Equals (str); }
private static final String sunflower = "Sunflower";
private static final int count= 1000000000;
public static void Main (string[] args) {try {thread.sleep (2000);
catch (Interruptedexception e) {e.printstacktrace ();
} System.out.println ("Start calculation .../n");
Long begin = System.currenttimemillis ();
for (int j = 0; J < COUNT; J +) {testemptybylength (sunflower);
} calculateduration ("Length test", begin);
Begin = System.currenttimemillis ();
for (int j = 0; J < COUNT; J +) {testemptybyempty (sunflower);
} calculateduration ("Empty Test", begin);
Begin = System.currenttimemillis ();
for (int j = 0; J < COUNT; J +) {testemptybyequals (sunflower);
} calculateduration ("Equals test", begin); public static void Calculateduration (String method, long begin) {System.out.println (method +/T:) + (SYSTEM.CU
Rrenttimemillis ()-Begin)/1000; public static Boolean testemptybylength (String str) {return str = NULL | | str.Length () < 1; The/** * Empty method is jdk1.6 added method if earlier versions are incompatible * * @param str * @return/public static Boolean Testemptybyempty (St
Ring str) {return str = NULL | | str.isempty ();
public static Boolean testemptybyequals (String str) {return ". Equals (str); }
Output results:
Start calculation (s)
Length test:0
Empty test:0
Equlas Test:8
Conclusion:
The String.isempty () method is added in jdk1.6, and its method body and length () method are better from performance and compatibility, length (),
So if the string is not strictly judged whether it is empty, it is recommended to use string.length>0 judgment, if the strict judgment is all "" character, it is necessary to write their own or with common Lang Stringutils.isblank
Isblank Method Code Supplement:
Java code/** * <p>Checks if a String is whitespace, empty ("") or null.</p> * * <pre> * stringutils.isblank (NULL) = true * stringutils.isblank ("") = true * stringutils.isblank (" ") = true * stringutils.isblank ("Bob") = false * stringutils.isblank (" bob ") = False * </pre> * * @param str the string to check, may be null *& NBsp; @return <code>true</code> if the String is null, empty Or whitespace * @since 2.0 */ public static boolean isblank (STRING&NBSP;STR) { int strLen; if (str == null | | (Strlen = str.length ()) == 0) { return true;  } for (int i = 0; i < strlen; i++) { if ((Character.iswhitespace (Str.charat (i)) == false)) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; return false; } } return true; }
/**
* <p>checks If a String is whitespace, empty ("") or null.</p>
*
* <pre>
* Stringuti Ls.isblank (null) = True
* Stringutils.isblank ("") = True
* Stringutils.isblank ("") = True
* Stringutils.isblank ("Bob") = False
* Stringutils.isblank (" bob ") = False
* </pre>
*
@param str the string to check, May is null
* @return <code>true</code> if the string Is null, empty or whitespace
* @since 2.0
*
/public static Boolean IsBlank (String str) {
int strlen;< c24/>if (str = NULL | | (StrLen = Str.length ()) = = 0) {return
true;
}
for (int i = 0; i < StrLen i++) {
if (Character.iswhitespace (Str.charat (i)) = False)} {return
false;
}
return
true;
}