StringBuffer help you lighten the burden of Java

Source: Internet
Author: User
Tags contains tostring stringbuffer
Working with text values is a routine task for programmers, typically using standard Java string classes to complete text-related requirements. It really works for a lot of small tasks, but if you're dealing with a large task, it consumes a lot of system resources. For this reason, the JDK introduced the StringBuffer class to provide a valid path for handling strings. Let's see how we can use this class to improve performance.

Why not use a standard string?


A Java string object is a constant string. Once the value is initialized and paid, its value and the allocated memory are fixed. If you want to change its value, a new string object will be generated that contains the new value. This is why a 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 substitution above. The first of these text is builder.com. Then, each time you add text, a new object is created.

The problem with this approach is that it consumes too much resources for such a simple process. In this case, the impact may be small (with very little code), but doing so in a large application with much more operations can degrade performance. This is the problem that the StringBuffer class is going to solve.

Handling Strings with StringBuffer
The StringBuffer class is designed to create and manipulate dynamic string information. The memory allocated for the object is automatically expanded to accommodate the new text. There are three ways to create a new StringBuffer object: Using initialization strings, sizing, and using 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, and the default capacity is 16 characters. The second instance of the class also contains no text, the capacity is 30 characters, and the last line creates an object that has an initialization value. The StringBuffer class is located in the Java.lang base package, so you do not need a special introduction statement to use it.

Once you create an object for the StringBuffer class, you can use a large number of methods and properties of the StringBuffer class. The most significant method is append, which adds text to the end of the current StringBuffer object's content. The following code examples 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 moves it to the standard output, but only one object is created. If you use a string object, you need more than eight objects. Note that the code takes advantage of the ToString method of the StringBuffer class. This method converts its contents into a string object that can be used for output. It allows the corresponding text of the operation to be used for output or data storage.

The Append method has 10 overloaded forms that allow various types of data to be added to the end of an object. The StringBuffer class also provides methods for working with object internal data.






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.