Stringbuffer and stringbuilder

Source: Internet
Author: User
Java. Lang. stringbuffer thread-safe variable character sequence. A string buffer similar to a string, but cannot be modified. Although it contains a specific character sequence at any time point, the length and content of the sequence can be changed by calling some methods. The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary, so all the operations on any specific instance are in serial order, this sequence is consistent with the method call sequence of each involved thread. The main operations on stringbuffer are append and insert methods. You can reload these methods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string into the string buffer. The append method always adds these characters to the end of the buffer, while the insert method adds the characters at the specified point. For example, if z references a string buffer object whose current content is "start", this method calls Z. append ("Le") causes the string buffer to contain "startle", while Z. insert (4, "Le") will change the string buffer to include "starlet ". Generally, if Sb references an instance of stringbuilder, SB. append (X) and SB. insert (sb. Length (), x) have the same effect. If an operation is performed on the source sequence (such as append or insert in the source sequence), the operation is only performed on the string buffer for this operation, rather than on the source. Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. From JDK 5, a equivalence class used by a single thread is added for this class, that is, stringbuilder. Compared with this class, the stringbuilder class should be used first, because it supports all the same operations, but because it does not execute synchronization, It is faster. Java. Lang. stringbuilder a variable character sequence. This class provides a stringbuffer-compatible API, but does not guarantee synchronization. This class is designed as a simple replacement of stringbuffer, used when the string buffer is used by a single thread (this is common ). If possible, we recommend that you use this class first, because in most implementations, It is faster than stringbuffer. The main operations on stringbuilder are append and insert methods. You can reload these methods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string to the string generator. The append method always adds these characters to the end of the generator, while the insert method adds the characters at the specified point. For example, if z references a string generator object whose current content is "start", this method calls Z. append ("Le") will make the string generator contain "startle", while Z. insert (4, "Le") changes the string generator to include "starlet ". Generally, if Sb references the stringbuilder instance, SB. append (X) and SB. insert (sb. Length (), x) have the same effect. Each string generator has a certain capacity. As long as the length of the Character Sequence contained in the string generator does not exceed this capacity, no new internal buffer needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. It is not safe to use stringbuilder instances for multiple threads. If such synchronization is required, stringbuffer is recommended.
From: http://zhidao.baidu.com/question/6823880.html

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.