StringBuffer class in Java

Source: Internet
Author: User

The StringBuffer class is the same as the String class and is also used to represent strings. It is only because the internal implementation of StringBuffer is different from that of String, so StringBuffer does not generate new objects during String processing, the memory usage is better than the String class.

Therefore, in actual use, if you often need to modify a string, such as insert and delete operations, StringBuffer is more suitable.

Many StringBuffer classes have the same methods as the String classes. These methods have the same functions as the String classes.

However, the most significant difference is that every modification to the StringBuffer object changes the object itself, which is the biggest difference with the String class.

In addition, because StringBuffer is thread-safe, there will be a special chapter on the concept of thread later, so it can be easily used in multi-threaded programs, however, the execution efficiency of the program is relatively slow.

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.

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. Common StringBuffer Methods
The methods in the StringBuffer class mainly focus on changing strings, such as append, insert, and delete. This is also the main difference between the StringBuffer and String classes.

A. append Method
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. For example:
StringBuffer sb = new StringBuffer ("abc ");
Sb. append (true );

The sb value of the object is changed to "abctrue ".

Using this method to connect strings saves more content than strings. For example, it is used to connect database SQL statements. For example:
StringBuffer sb = new StringBuffer ();
String user = "test ";
String pwd = "123 ";
Sb. append ("select * from userInfo where username =")
. Append (user)
. Append ("and pwd = ")
. Append (pwd );
In this way, the sb value of the object is the string "select * from userInfo where username = test and pwd = 123 ".

B. deleteCharAt Method
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. For example:
StringBuffer sb = new StringBuffer ("Test ");
Sb. deleteCharAt (1 );

The purpose of this Code is to delete the character whose index value is 1 in the string object sb, that is, to delete the second character. The remaining content forms a new string. Therefore, the sb value of the object is changed to "Tst ".

There is also a function-like delete method:
Public StringBuffer delete (int start, int end)

This method is used to delete all characters within the specified range, including start, excluding the range of the end index value. For example:
StringBuffer sb = new StringBuffer ("TestString ");
Sb. delete (1, 4 );
The purpose of this Code is to delete all characters between index value 1 (inclusive) and index value 4 (excluded), and the remaining characters form a new string. The sb value of the object is TString ".

C. insert Method
Public StringBuffer insert (int offset, boolean B)
This method inserts content into the StringBuffer object and then forms a new string. For example:
StringBuffer sb = new StringBuffer ("TestString ");
Sb. insert (4, false );
The code in this example inserts the false value at the index value 4 of the object sb to form a new string. After the execution, the sb value of the object is "TestfalseString ".

D. reverse Method
Public StringBuffer reverse ()
This method is used to reverse the content in the StringBuffer object and form a new string. For example:
StringBuffer sb = new StringBuffer ("abc ");
Sb. reverse ();
After the inversion, the content in the object sb will become "CBA ".

E. setCharAt Method
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. For example:
StringBuffer sb = new StringBuffer ("abc ");
Sb. setCharAt (1, 'D ')
The sb value of the object is changed to "aDc ".

F. trimToSize Method
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.

In short, in actual use, String and StringBuffer have their own advantages and disadvantages. You can select the corresponding type based on the specific use environment.

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.