From source Analysis Java.lang.String.isEmpty ()

Source: Internet
Author: User

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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.