StringBuffer categories (String, StringBuffer, StringBuilder difference)

Source: Internet
Author: User
Tags stringbuffer

StringBuffer class 1. Class structure:

· Java.lang.Object

· Java.lang.StringBuffer

· all implemented interfaces:

Serializable, Appendable, charsequence

Public final classStringBuffer

Extendsobject

Implementsserializable, Charsequence 2. class overview:

A string buffer similar to strings, but cannot be modified. Although it contains a particular sequence of characters at any point in time, some method calls can change the length and content of the sequence.

You can safely use string buffers with multiple threads. These methods can be synchronized as necessary, so all operations on any particular instance occur as if they were in a sequential order, in the same order as the method calls of each of the involved threads.

The main operations on StringBuffer are the Append and insert methods, which can be overloaded to accept any type of data. Each method effectively converts the given data to a string, and then appends or inserts the character of the string into the string buffer. The Append method always adds these characters to the end of the buffer, and the Insert method adds the characters at the specified point.

Each string buffer has a certain capacity. You do not need to allocate a new internal buffer array as long as the string buffer contains a sequence of characters that does not exceed this capacity. This capacity is automatically increased if the internal buffer overflows. Starting with JDK 5, the class is supplemented by an equivalence class used by a single thread, that is, StringBuilder. As opposed to this class, the StringBuilder class should generally be used preferentially because it supports all the same operations, but is faster because it does not perform synchronization.

3. Construction Method

Public Constructors

StringBuffer ()

Constructs a string buffer with no characters in it, with an initial capacity of 16 characters.

StringBuffer (int capacity)

Constructs a string buffer with no characters, but with the specified initial capacity.

StringBuffer (String string)

Constructs a string buffer and initializes its contents to the specified string content.

StringBuffer (Charsequence CS)

Constructs a string buffer that contains the same characters as the specified charsequence.

4. Common methods:

Public Methods

StringBuffer

Append (double D)

Appends the string representation of the double parameter to this sequence.

StringBuffer

Append (Boolean B)

Appends the string representation of a Boolean parameter to a sequence.

StringBuffer

Append (Long L)

Appends the string representation of a long argument to this sequence.

StringBuffer

Append (float f)

Appends the string representation of the float parameter to this sequence.

StringBuffer

Append (int i)

Appends the string representation of an int parameter to this sequence.

StringBuffer

Appendcodepoint (int codepoint)

Appends the string representation of the codepoint parameter to this sequence.

int

Capacity ()

Returns the current capacity.

StringBuffer

Delete (int start, int end)

Removes characters from substrings in this sequence.

StringBuffer

Deletecharat (int index)

Removes char from the specified position of this sequence.

int

IndexOf (String string)

Returns the index of the first occurrence of the specified substring in the string.

StringBuffer

Insert (int index, int i)

Inserts the string representation of the int parameter into this sequence.

StringBuffer

Insert (int index, double D)

Inserts the string representation of the double parameter into this sequence.

StringBuffer

Insert (int index, long L)

Inserts the string representation of a long argument into this sequence.

StringBuffer

Insert (int index, Object obj)

Inserts the string representation of the object argument into this sequence of characters.

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.