The difference between java:string, StringBuffer and StringBuilder

Source: Internet
Author: User

During the company internship, slowly realized as a developer, the company's code convention is how important.

The following article is listed by the company.

1. Rather than "String", "StringBuffer" is recommended to being used to concatenate character strings.

Especially when a string was constructed by concatenating data in a loop, or when a string content was changed for numerous Amount of times before it is returned/displayed, "StringBuffer" should be used. Sample code could is like:

StringBuffer StringBuffer = new StringBuffer ();

for (int i=0; i<100; i++) {

Stringbuffer.append ("sample string");

}

return stringbuffer.tostring ();

In general, it is recommended that you use StringBuffer when connecting strings, rather than string. This is especially true when a string is used to concatenate data in a loop, or if its contents need to be modified frequently before the string is returned, StringBuffer should be used.

Checked the information on the Internet. In addition to other people's reference, the following is their own summary section. Hope to help everyone.

1. String: It is an immutable (immutable) object, and when the string object is changed it is actually equivalent to generating a new string object and then pointing the reference to the new string object, the original string object GC is recycled.

2. StringBuffer string variable (thread safe), for multi-threaded programs, to ensure synchronization, if you want to frequently modify the contents of the string, for efficiency considerations best Use StringBuffer, if you want to turn to string type, You can call the ToString () method of StringBuffer.

2.1 Initialization:

New StringBuffer ("abc");

2.2 Conversions: It is important to note that StringBuffer and string are of different types and cannot be cast directly, and the following code is wrong:

// assignment types do not match  = (stringbuffer) "ABC";    // There is no inheritance relationship and cannot be strongly turned

The code for 2.3 between the StringBuffer object and the string object is as follows:

String s =new stringbuffer ("123new StringBuffer (s);   // convert string to StringBuffer  = sb1.tostring ();              // StringBuffer converted to string. ToString ()

StringBuilder string variable (non-thread safe), suitable for single-threaded programs, does not guarantee synchronization. Briefly, the main performance difference between the String class and the Stringbuffer/stringbuilder class is that the string is an immutable object, so each time a change is made to the string class it is equivalent to generating a new string object, but The pointer is then pointed to the new string object, so the string that changes the content often is best not to use string and the result is different if you use the Stringbuffer/stringbuilder class, each time the result will be stringbuffer/ StringBuilder the object itself, rather than generating a new object, and then changing the object reference. Therefore, in general, it is recommended to use Stringbuffer/stringbuilder, especially if the string object changes frequently.

Usage: Append,insert, remove,replace, delete

New StringBuffer ("teststring"); sb. Delete (1,4);

The function of the above code is to remove all characters between the index value 1 (including) to the index value 4 (not included), and the remaining characters to form a new string. The value of the object SB is "Tstring".

Common operations for both: Append (), toString (), insert (), reverse (), delete (), and so on.
In most cases StringBuilder > Stringbuffe think that the former does not need to consider thread safety.

The difference between java:string, StringBuffer and StringBuilder

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.