Processing text values is the daily work of programmers. Generally, standard Java string classes are used to meet text-related requirements. It is applicable to many small tasks, but if it is a large task, it will consume a lot of system resources. For this reason, JDK introduces the stringbuffer class to provide a valid path for processing strings. Let's take a look at how to use this class to improve performance.
Why not use a standard string?
The Java String object is a constant string. Once the value is initialized and paid, its value and allocated memory are fixed. To change its value, a New String object containing the new value is generated. This is why the string object consumes a lot of resources. The following code creates a String object and uses the concatenation (+) symbol to add more characters to it:
String sample1 = new string ("builder.com ");
Sample1 + = "is ";
Sample1 + = "the place ";
Sample1 + = "to be .";
The system will eventually create four string objects to complete the above replacement. The first text is builder.com. Then, a new object is created every time the text is added.
The problem with this method is that it consumes too much resources for such a simple process. In this example, the impact may be very small (that is, a small amount of code is given), but doing so in a large application with a lot of operations will degrade the performance. The stringbuffer class must solve this problem.
Use stringbuffer to process strings
The stringbuffer class is designed to use and create and operate dynamic string information. The memory allocated to this object is automatically extended to accommodate the new text. There are three ways to create a new stringbuffer object: Use the initialization string, set the size, and use the default constructor:
Stringbuffer sb = new stringbuffer ();
Stringbuffer sb = new stringbuffer (30 );
Stringbuffer sb = new stringbuffer ("builder.com ");
The first line creates an object that does not contain any text. The default size is 16 characters. The second instance of the class does not contain text, and the size is 30 characters. The last row creates an object with the initialization value. The stringbuffer class is located in the basic java. lang package. Therefore, you do not need to introduce special statements to use it.
Once a stringbuffer class object is created, a large number of methods and attributes of the stringbuffer class can be used. The most notable method is append, which adds text to the end of the content of the current stringbuffer object. The following code demonstrates the syntax of the append method:
Stringbuffer sb = new stringbuffer ();
SB. append ("B ");
SB. append ("U ");
SB. append ("I ");
SB. append ("L ");
SB. append ("D ");
SB. append ("E ");
SB. append ("R ");
SB. append (". com ");
System. Out. println (sb. tostring ());
The Code creates the builder.com string and sends it to the standard output, but only creates one object. If a String object is used, more than eight objects are required. Note that the Code uses the tostring method of the stringbuffer class. This method converts the content into a string object that can be used for output. It allows operations on the corresponding text for output or data storage.
The append method has ten reloads, allowing you to add various types of data to the end of an object. The stringbuffer class also provides methods to process internal data of objects.