String and stringbuilder)

Source: Internet
Author: User

Reading Li Jianzhong Instructor'sProxyDuring the Agent Mode Course, I heard about it.StringType andStringbuilderType. We have a new understanding of the implementation of these two types.

I have read aboutStringType of data,StringType, it gives me the impression that it is used with caution, otherwise the memory will produce a lot of garbage. The key issue lies inStringObject modification.

When we instantiateStringIn memory, allocate a space for this object. As follows:

String strtest = "AA ";

When we modifyStrtestFor example:Strtest = "BB ";In this caseStrtestAllocate a new space. In this way, the original memory space is wasted and can only be recycled by the garbage collector. I found someCodeFrequently modifiedStringObject value, for example:

String strsql = "Select ....";

Strsql + = "...";

Strsql + = "...";

Strsql + = "...";

...

In fact, this is a waste of memory space. If this is the case frequently, we recommend that you useStringbuilderObject, but if the number of times the string is frequently accumulated is not many, it is actually usedStringbuilderIt is counterproductive. I think this can be written in the following situations:

String strsql = "Select..." + "..." + ...;

This will provide better performance.

This time Li Jianzhong The instructor explainedCopy-to-writeTechnology. For twoStringType variables are as follows:

String str1 = "AA ";

String str2 = "AA ";

When the twoStringWhen the values of type variables are the same, they actually point to the same memory space, for example:


ForStringbuilderThe object is like this, for example, there are twoStringbuilderObject

Stringbuilder SB1 = new stringbuilder ("AA ")

Stringbuilder sb2 = new stringbuilder ("AA ")

Their status in memory is as follows:

 

WhenSb2Only modifySb2. For example

From: http://www.cnblogs.com/wayne-ivan/archive/2006/10/18/532320.html

PS: I am also a student of Li Jianzhong, so of course I can support it!

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.