The StringBuffer class, like String, is also used to represent strings, but because StringBuffer are implemented in a different way than string, StringBuffer does not generate new objects when string processing. superior to the string class in memory usage.
Therefore, in actual use, if you often need to modify a string, such as INSERT, delete, and other operations, the use of stringbuffer to be more appropriate.
There are many methods in the StringBuffer class that are the same as string classes, which are functionally identical to those in the string class.
One of the most notable differences, however, is that each modification of the StringBuffer object changes the object itself, which is the biggest difference from the string class.
In addition, because the StringBuffer is thread-safe, the concept of thread on the follow-up has a special chapter to introduce, so in multithreaded programs can also be very convenient to use, but the implementation of the program efficiency is relatively slow.
1, the initialization of the StringBuffer object
The initialization of a StringBuffer object is not the same as the initialization of a string class, Java provides a special syntax, and typically the constructor is used for initialization.
For example:
StringBuffer s = new StringBuffer ();
The StringBuffer object initialized is an empty object.
If you need to create a StringBuffer object with content, you can use:
StringBuffer s = new StringBuffer ("abc");
The content of this initialized StringBuffer object is the string "abc".
It should be noted that StringBuffer and string are of different types and cannot be coerced directly, and the following code is wrong:
StringBuffer s = "abc"; Assignment types do not match
StringBuffer s = (stringbuffer) "ABC"; No inheritance relationship, no strong transfer
The code for the interchange between the StringBuffer object and the string object is as follows:
String s = "abc";
StringBuffer sb1 = new StringBuffer ("123");
StringBuffer SB2 = new StringBuffer (s); string conversion to StringBuffer
String S1 = sb1.tostring (); Convert StringBuffer to String
2, the common method of StringBuffer
The methods in the StringBuffer class mainly focus on changes to strings, such as Append, insert, and delete, which is also the main difference between the StringBuffer and string classes.
A, Append method
Public StringBuffer Append (Boolean B)
The purpose of this method is to append content to the end of the current StringBuffer object, similar to a string connection. After the method is invoked, the contents of the StringBuffer object also change, for example:
StringBuffer sb = new StringBuffer ("abc");
Sb.append (TRUE);
The value of the object SB will become "Abctrue".
Using this method for string concatenation will save more content than string, such as a connection that applies to a database SQL statement, 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);
The value of this object SB is the string "select * from UserInfo where username=test and pwd=123."
B, Deletecharat method
Public stringbuffer deletecharat (int index)
The purpose of this method is to delete the character 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 code's role is to remove the character with index value 1 in SB of the string object, that is, to delete the second character and the remainder to form a new string. So the value of the object SB is changed to "Tst".
There is also a function-like Delete method:
Public StringBuffer Delete (int start,int end)
The effect of this method is to delete all characters within the specified interval, including start, and the interval that does not contain 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 the index value 1 (including) and the index value 4 (excluding), and the remaining characters to form a new string. The value of the object SB is "Tstring".
C, Insert method
Public stringbuffer Insert (int offset, Boolean b)
The purpose of this method is to insert content into the StringBuffer object and then form a new string. For example:
StringBuffer sb = new StringBuffer ("teststring");
Sb.insert (4,false);
The example code is to insert a false value at the location of the index value 4 of the object SB to form a new string, then the value of the object SB after execution is "testfalsestring".
D, Reverse method
Public StringBuffer Reverse ()
The purpose of this method is to invert the contents of the StringBuffer object and then form a new string. For example:
StringBuffer sb = new StringBuffer ("abc");
Sb.reverse ();
After the reversal, the contents of the object SB will become "CBA".
E, Setcharat method
public void Setcharat (int index, char ch)
The purpose of this method is to modify the character of the index value in the object to be the new character Ch. For example:
StringBuffer sb = new StringBuffer ("abc");
Sb.setcharat (1, ' D ');
The value of the object SB will become "ADc".
F, TrimToSize method
public void TrimToSize ()
The purpose of this method is to reduce the space waste by narrowing the storage space in the StringBuffer object to the same length as the string length.
in summary, in practice, string and stringbuffer each have advantages and disadvantages, according to the specific use of the environment, Select the corresponding type to use.