The difference between the IsEmpty method, NULL, and "" of the string class in Java

Source: Internet
Author: User

Reference: http://blog.csdn.net/violetjack0808/article/details/50668195

Content:

All along the string of these three empty quite dizzy, just colleague asked me, I also study under.

What you see on someone's blog is this:
IsEmpty ()
The memory space is allocated, the value is empty, is absolute null, is a value (value = null)
""
Allocated memory space, value is empty string, is relative empty, is a value (value = empty string)
Null
is unallocated memory space, no value, is an no value (value does not exist)

However, I was quite dizzy, so I wrote a demo to verify the next ~

 @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main);    String a = new string ();    String B = "";    String c = null;    TestString (a);    TestString (b); TestString (c);}    private void TestString (String str) {if (str = = null) {LOG.E ("testnull", "null");    } if (Str.isempty ()) {LOG.E ("Testnull", "IsEmpty");    } if (Str.equals (")") {LOG.E ("testnull", "quotation marks"); } log.e ("Testnull", "----------------");} 


Because it only plays Android, it writes an Android demo. The effect is the same ~
The log results are:
02-15 14:41:10.655 31875-31875/uding.testanimation E/testnull:isempty
02-15 14:41:10.655 31875-31875/uding.testanimation e/testnull: quotation marks
02-15 14:41:10.655 31875-31875/uding.testanimation e/testnull:----------------
02-15 14:41:10.655 31875-31875/uding.testanimation E/testnull:isempty
02-15 14:41:10.655 31875-31875/uding.testanimation e/testnull: quotation marks
02-15 14:41:10.655 31875-31875/uding.testanimation e/testnull:----------------
02-15 14:41:10.655 31875-31875/uding.testanimation E/testnull:null

Then STR reported null pointer Error ~ Here can be found as if isempty and "" effect is the same drop AH.
And looked at the source of the next IsEmpty:

public Boolean isEmpty () {    return count = = 0;}

This means that as long as the text number of string is 0 to return true, and "" The number of text is 0.

Sum up:
Null is the "empty" of unallocated memory space ~
IsEmpty and quotation marks are two "empty" relative to null (that is, they!). = null), and both are allocated memory (as if almost a meaning)

(PS: It is unclear isempty and "" Whether there is any difference, there is a small partner to know that can be commented, thank you ~)

The difference between the IsEmpty method, NULL, and "" of the string class in Java

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.