Today in writing code to use the Java.lang.String.isEmpty () of this method, before also used, today whim, looked at the source code, understand its realization method, summed up, we can exchange.
Normally, this is the way we use this method:
"Hello Wudb". IsEmpty ();
The above code returns false, then we open source code analysis, IsEmpty () This method in many classes have, we analyzed today is a string inside, so find java.lang.String this class, and then go to find Idempty () This method
/*** Returns {@codetrue} if, and only if, {@link#length ()} is {@code0}. * *@return {@codetrue} if {@link#length ()} is {@code0}, otherwise * {@codefalse} * *@since1.6*/ Public BooleanIsEmpty () {returnValue.length = = 0; }
The source has been very clear, when and only if the length of the string is 0 to return true, otherwise the return is false the values of the two Boolean types, the method appears in the value of what is, continue to find
/***/ privatefinalchar value[];
A one-dimensional array of type char is defined above the string, so you can see that the implementation of string is based on the char type (which is actually a sequence of Unicode characters). This is also reflected in the length () of another method of stirng:
/** * Returns The length of this string. * The length is equal to the number of <a href= ' Character.html#unicode ' >unicode * Code units</a> in the St Ring. * @returnThe length of the sequence of characters represented by this * object.< c12/>* /publicint Length () {return value.length; }
The string length here is also used for the length property of the char array.
So when the string is "" ". IsEmpty returns True, Null.isempty is an error when the string is null. So when using the IsEmpty method, Make sure that the string is not null .
Outside the work to see the source code is still very helpful, I see on the internet there is a discussion of the difference between null, "" and isempty, in fact, like this problem, we can completely by reading the source to solve.
From source Analysis Java.lang.String.isEmpty ()