Java record-29-StringBuffer

Source: Internet
Author: User

Java record-29-StringBuffer
StringBufferString is a constant final and cannot be appended when it is created. Therefore, a large number of objects will be created when a large number of strings are used. To avoid creating too many objects, with StringBuffer, StringBuffer is similar to the String buffer, but can be modified. When multiple strings are concatenated, only one StringBuffer object is generated. Its important method append () returns the reference of its original object, and can append any native type and convert it to a string. Example: StringBuffer sb = new StringBuffer (); sb. append ("hello "). append ("world "). append ("welcome "). append (1, 100 ). append (false ). string s = sb. toString (); java. lang. stringBufferpublic final class StringBuffer extends Object implements Serializable, CharSequence thread-safe variable character sequence. A String buffer similar to a String, but can 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 with the current content of "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. When an operation related to the source sequence (such as an append or insert operation in the source sequence) occurs, 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.
Next article: http://www.bkjia.com/kf/201510/447807.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.