Stringbuffer and stringbuilder for Java Development (1)

Source: Internet
Author: User
Turn: http://www.jcourse.cn/read/2342009-03-02 Author:

Zhangtaolv

In our daily development, we often encounter text operations. In general MIS systems, there are many text operations, such as user names, article content, article title, and contract content. In the previous tutorial, I also explained the most basic string operation class in Java, String. In the previous tutorial, I also told you that string is an unchangeable class, that is, its content cannot be changed after construction, so the content is frequently changed (for example, we edit the article) string is not suitable. So what should we do when we encounter such a situation? Java provides stringbuilder and stringbuffer for us.

First, let's talk about what they have in common! Basically, we use them when the strings change frequently, and then using them will be faster than using the string class. The difference between the two classes is that stringbuffer is thread-safe while stringbuilder is non-thread-safe.

After a general explanation, let's take a look at how to create stringbuffer from an existing String object and convert stringbuffer to a string object.

First, the stringbuffer class constructor API is provided. Let's see how many methods can be used to construct it.

Constructor Summary
Stringbuffer ()
Construct a string buffer with no characters. Its initial capacity is 16 characters.
Stringbuffer (Charsequence SEQ)
Public java. Lang. stringbuilder (charsequence SEQ) constructs a string buffer that contains the same characters as the specified charsequence.
Stringbuffer (INT capacity)
Construct a string buffer with no characters but a specified initial capacity.
Stringbuffer (String Str)
Constructs a string buffer and initializes its content to the specified string content.
 

We can see that we can either directly add a new parameter or provide a parameter during the new process. The parameter can be a string, a capacity value, or a charsequence. First, let's take a look at the stringbuffer construction using an existing string. The following is an example:

Java code
  1. Public class mainclass {
  2. Public static void main (string [] Arg ){
  3. Stringbuffer astring = new stringbuffer ("ABCDE ");
  4. String phrase = "abced ";
  5. Stringbuffer buffer = new stringbuffer (phrase );
  6. System. Out. println (astring );
  7. System. Out. println (buffer );
  8. }
  9. }

In the above Code, we use an existing String object to create two stringbuffer objects, and then we directly pass them as parameters to the println method. Here we can see that they actually output strings represented by stringbuffer. In fact, when we use stringbuffer to construct a string, we will still call the tostring method to obtain the output, so as to obtain the string we actually need.

Now, let's take an example of converting stringbuffer into a string. This is simple:

Java code
  1. Public class mainclass {
  2. Public static void main (string [] Arg ){
  3. Stringbuffer palindrome = new stringbuffer ("so far dynamos ");
  4. String astring = palindrome. tostring ();
  5. System. Out. println (astring );
  6. }
  7. }

As you can see, we often use the tostring method in daily development, especially when using stringbuffer to spell SQL.

From the above constring method, we can see that when constructing a stringbuffer object, we can also specify the capacity of the stringbuffer, which is specified by Int. As can be seen in the above API, if this parameter is not specified, the default value is 16 characters. Let's give an example:

Java code
  1. Public class mainclass {
  2. Public static void main (string [] Arg ){
  3. Stringbuffer newstring = new stringbuffer (50 );
  4. System. Out. println (newstring. Capacity ());
  5. }
  6. }

In the above example, we have constructed a stringbuffer object and assigned a value of 50 to its capacity, so that if we add less than 50 characters next to it, there will be no new capacity expansion. The above example also shows that capacity can be called to obtain the capacity information of the current stringbuffer object. Let's take an example to see how the stringbuffer capacity changes:

Java code
  1. /**
  2. * User: zhangtao
  3. * Date: 2009-3-2
  4. * Time: 16:24:36
  5. */
  6. Public class stringbufferdemo {
  7. Public static void main (string [] ARGs ){
  8. Stringbuffer sb = new stringbuffer (); // The default capacity is 16
  9. System. Out. println (sb. Capacity (); // output 16
  10. SB. append( "123456789012345 ");
  11. System. Out. println (sb. Capacity (); // output 16
  12. SB. append ("6 ");
  13. System. Out. println (sb. Capacity (); // output 16
  14. SB. append( "789111 ");
  15. System. Out. println (sb. Capacity (); // output 34
  16. }
  17. }

The final output is based on the following: int newcapacity = (value. length + 1) * 2; this is the basis of internal computation, that is, after the value is exceeded, the capacity is calculated first, then 1 is added, and then 2 is multiplied, so the output is 17*2 = 34. With this formula, I believe you will know the next capacity. That's right, it's 70.

Well, this is a little bit more about it. First come here, we will explain other methods in detail below.

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.