Relationship between stringbuilder, stringbuffer, and string

Source: Internet
Author: User

Fully explain the relationship between stringbuilder, stringbuffer, and string
1. string class
  The string value is immutable, which leads to a new String object generated every time a string operation is performed, which is not only inefficient, but also a large waste of limited memory space.
  String
A = "A"; // assume a points to address 0x0001
  A
= "B"; // After the value is re-assigned, a points to address 0x0002, but the "A" stored in address 0x0001 still exists, but it is no longer pointed to by, A has pointed to another address.
  Therefore, the string operation changes the value assignment address rather than the value change operation.

2. stringbuffer is a variable class and thread-safe string operation class. Any operation on the string to which it points will not produce new objects. Each stringbuffer object has a certain buffer capacity. When the string size does not exceed the capacity, no new capacity is allocated. When the string size exceeds the capacity, the capacity is automatically increased.

  Stringbuffer Buf = new stringbuffer (); // allocate a 16-Byte Character Buffer
  Stringbuffer Buf = new stringbuffer (512); // allocate a 512-Byte Character Buffer
  Stringbuffer Buf = new stringbuffer ("this is a test") // stores strings in the buffer and reserves a 16-byte empty buffer.

3. stringbuffer
The stringbuffer and stringbuilder classes have similar functions. The main difference is that the stringbuffer class methods are multithreading and secure, while stringbuilder is NOT thread-safe. In contrast, the stringbuilder class is slightly faster. The stringbuffer and stringbuilder classes should be used for strings that frequently change values.

4. Thread Security
Stringbuffer thread security
Stringbuilder thread is not secure

5. Speed
In general, the speed is from fast to slow: stringbuilder> stringbuffer> string. This comparison is relative, not absolute.

6. Summary
(1). If you want to operate a small amount of data, use = string
(2). A single thread operates on a large amount of data in the string buffer = stringbuilder
(3). multi-threaded operation of a large amount of data under the string buffer = stringbuffer

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.