In the previous sections we learned about the String object. The string object created by the String class cannot be modified. That is to say, the String string cannot modify, delete or replace a character in the string. That is, once the String object is created , its entity cannot change, for example:
Strings=newString(I like walking);
Among them, I like to walk as an entity and can no longer change.
So in this section we will learn about the StringBuffer class , which can create a modifiable string sequence. That is to say, the physical memory space of the object of this class can automatically change the size to facilitate the storage of a variable character sequence.
For example, a StringBuffer object can append a sequence of characters by calling the append method:
StringBuffers=newStringBuffer(I like);
Then, object s can call the append method to append a string sequence:
s.append(swimming);
Among them, I like swimming as an entity, and the entity changes.
The StringBuffer class has three constructors:
Use the first parameterless constructor to create a StringBuffer object, then the initial capacity of the entity allocated to the object can hold 16 characters. When the length of the character sequence stored in the entity of the object is greater than 16, the capacity of the entity is automatically increased. to store the added characters. The StringBuffer object can obtain the length of the character sequence stored in the entity through the length() method, and obtain the actual capacity of the current entity through the capacity() method.
Use the second constructor to create a StringBuffer object, then you can specify that the initial capacity of the entity allocated to the object is the number of characters specified by the parameter size. When the length of the character sequence stored in the entity of the object is greater than size characters, the entity The capacity is automatically increased to accommodate the added characters.
Using the third constructor method to create a StringBuffer object, you can specify that the initial capacity of the entity allocated to the object is the length of the parameter string s plus an additional 16 characters.