String and stringbuffer

Source: Internet
Author: User

String operates on a string of characters. It is a final class and cannot be inherited. It is an immutable class. Once it is created, its value cannot be modified, to modify an existing String object, create a new object and save the new value.

Stringbuffer also operates on a string of characters, but the variable class changes the object itself every time it is modified. It can only be created by constructor, and cannot be assigned by a value symbol (for example, SB = "Welcome to here! "; // Error ). In addition, stringbuffer is thread-safe, so it can be conveniently used in multi-threaded programs.

 

Stringbuffer is more efficient than string connection operations.

String STR = new string ("Welcome ");
STR + = "here ";
The above process is actually performed by creating a stringbuffer, letting Hou call append (), and then stringbuffer tosting (); in this case, the string connection operation has some additional operations than the stringbuffer operation, of course, the efficiency is reduced, and because the string object is an immutable object, each operation of sting creates a new object to save the new value. In this way, the original object will be useless and it will be recycled, which will also affect the performance.

 

 1. stringbuffer object initialization

The initialization of the stringbuffer object is not the same as the initialization of the string class. Java provides special syntax, and generally uses the constructor for initialization.

For example:

Stringbuffer S = new stringbuffer (); in this way, the initialized stringbuffer object is an empty object.

If you need to create a stringbuffer object with content, you can use: stringbuffer S = new stringbuffer ("ABC"); in this way, the content of the initialized stringbuffer object is the string "ABC ".

Note that stringbuffer and string belong to different types and cannot be forced to convert them directly. The following code is incorrect:

Stringbuffer S = "ABC"; // The Value assignment type does not match.

Stringbuffer S = (stringbuffer) "ABC"; // The inheritance relationship does not exist and cannot be forced.

The code for converting stringbuffer objects and string objects is as follows:

String S = "ABC ";

Stringbuffer SB1 = new stringbuffer ("123 ");

Stringbuffer sb2 = new stringbuffer (s); // string to stringbuffer

String S1 = sb1.tostring (); // convert stringbuffer to string

 2. append method of stringbuffer

Public stringbuffer append (Boolean B)

This method is used to Append content to the end of the current stringbuffer object, similar to a string connection. After this method is called, the content of the stringbuffer object also changes.

 3. deletecharat method of stringbuffer

Public stringbuffer deletecharat (INT index)

The function of this method is to delete the characters at the specified position, and then form the remaining content into a new string.

 4. insert method of stringbuffer

Public stringbuffer insert (INT offset, Boolean B)

This method inserts content into the stringbuffer object and then forms a new string.

 5. Reverse Method of stringbuffer

Public stringbuffer reverse ()

This method is used to reverse the content in the stringbuffer object and form a new string.

 6. setcharat method of stringbuffer

Public void setcharat (INT index, char ch)

The function of this method is to modify the index value of an object to the index position of the new character ch.

 7. trimtosize method of stringbuffer

Public void trimtosize ()

The function of this method is to reduce the storage space in the stringbuffer object to the same length as the string length, reducing the waste of space.

String and 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.