StringBuffer helps you relieve Java burden

Source: Internet
Author: User
Tags processing text

 


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.
Capacity of StringBuffer

You can use the capacity and length methods to set the number of characters in an object. The capacity method returns the number of characters (memory) allocated to the object ). If the capacity is exceeded, it will automatically expand to meet the requirements. The length method returns the number of characters currently stored in the object. You can use the setLength method to increase the length. In addition, the object capacity can be expanded using the ensureCapacity method. It establishes the minimum capacity of the object, so if it exceeds the limit, there will be no problem. The following code uses these methods:

StringBuffer sb = new StringBuffer ();

Sb. ensureCapacity (40 );

Sb. append ("Builder.com is awesome !");
Systrm. out. println (sb. toString ());

Sb. setLength (11 );

Systrm. out. println (sb. toString ());

The Code sets the string capacity and pays for it. The length attribute is reset, so the text is truncated. The input result is as follows:

Builder.com is awesome!

Builder.com

Operation string
There are more methods to process strings stored in the StringBuffer object. The following are examples:

CharAt returns a single character in the string.
SetCharAt is the value or replacement of a single character in the string.
GetChars returns a substring of the string.
Insert inserts a value at the specified position of the string. It has multiple overloaded versions to accommodate various data types.
Substring returns a subset of the string.
Reverse invert the content of StringBuffer.
All methods are useful for Operation values, but the reverse method is the coolest-it makes it easy to put a string upside down with only one call. The following code and result are used as an example:

StringBuffer sb = new StringBuffer ();

Sb. ensureCapacity (100 );

Sb. append ("Builder.com !");

System. out. println (sb. toString ());

Sb. reverse ();

Systrm. out. println (sb. toString ());

Output:
Builder.com!

! Moc. redliuB

StringBuffer advantages
The use of strings runs through most applications, whether as a user interface identifier or as a value retrieved from the database in the background. Generally, these values do not meet the requirements and need to be processed. You can use the String class, but it is not designed to process dynamic values. The StringBuffer class fills this requirement and makes the use of system resources more effective.

 

Related Article

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.