Principle and common methods of Java StringBuilder class

Time:2021-7-15

This article mainly introduces the principle and common methods of Java StringBuilder class. The example code is introduced in great detail, which has a certain reference learning value for everyone’s study or work. If you need a friend, you can refer to it

The origin of StringBuilder class

Because the object content of string class cannot be changed (the bottom layer is an array decorated by final), whenever we splice strings, we always create a new object in memory. If the string is spliced, a new string object will be constructed every time, which is time-consuming and space-consuming. To solve this problem, we can use the Java. Lang. StringBuilder class.

Introduction of StringBuilder

Look up the API of java.lang.stringbuilder. StringBuilder is also called variable character sequence. It is a string buffer similar to string. The length and content of the sequence can be changed by some method calls. The original StringBuilder is a string buffer, that is, it is a container, which can hold many strings. And can carry on each kind of operation to its string. It has an array inside to store the string content. When splicing strings, new content is directly added to the array. StringBuilder will automatically maintain the expansion of the array (default 16 character space, exceeding the automatic expansion).

There are two common construction methods

Public StringBuilder (): constructs an empty StringBuilder container.

Public StringBuilder (string STR): constructs a StringBuilder container and adds strings to it.

There are two common methods of StringBuilder

Public StringBuilder append (…): adds a string form of any type of data and returns the current object itself. Any data as a parameter will add the corresponding string content to StringBuilder.

package demo06;

public class Demo01StringBuilder {
  public static void main(String[] args) {
    //Create StringBuilder object
    StringBuilder bu = new StringBuilder();
    //Using the append method to add data to StringBuilder, you can add any type of data
    /*
      Chain programming: method return value is an object, can continue to call the method
     */
    Bu. Append ("ABC"). Append (1). Append (true). Append (8.8). Append ('middle ');
    System.out.println(bu);// Abc1true8.8

  }
}

Public string tostring() ‘: converts the current StringBuilder object to a string object.

package demo06;

public class Demo02StringBuilder {
  /*
  StringBuilder and string can be converted to each other
    String > StringBuilder: you can use the construction method of StringBuilder
      StringBuilder (string STR) constructs a string generator and initializes it to the specified string content.
    StringBuilder > string: you can use the toString method in StringBuilder
      Public string tostring(): converts the current StringBuilder object to a string object.
 */
  public static void main(String[] args) {
    //String->StringBuilder
    String str = "hello";
    StringBuilder bu = new StringBuilder(str);
    //Add data to StringBuilder
    bu.append("world");
    System.out.println("StringBuilder:" + bu);
    //StringBuilder->String
    String s = bu.toString();
    System. Out. Println ("string: + s)";
  }
}

Results after code execution

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.