String splicing problem
Because the object content of the String class cannot be changed, a new object is always created in memory whenever String splicing is performed. For example:
public static void main(String[] args) { String s = "Hello"; s += "World"; System.out.println(s); }
The String class is described in the API as follows: strings are constants and their values cannot be changed after creation.
After analyzing our code according to this sentence, we have generated three strings, namely "hello", "World" and "HelloWorld". The reference variable s first points to the Hello object, and finally points to the spliced new string object, namely, HelloWord.
Therefore, if the String is spliced, a new String object will be built every time, which is time-consuming and waste space. To solve this problem, you can use the java.lang.StringBuilder class.
StringBuilder overview
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 buffer of strings, that is, it is a container in which many strings can be contained. And it can perform various operations on the strings.
It has an array inside to store string content. When string splicing, new content is added directly to the array. StringBuilder will automatically maintain the expansion of the array.
The principle is shown in the following figure: (the default 16 character space exceeds the automatic expansion)
Construction method
According to the API documents of StringBuilder, there are two common construction methods:
public StringBuilder(): construct an empty StringBuilder container.
public StringBuilder(String str): construct a StringBuilder container and add strings.
public static void main(String[] args) { StringBuilder sb1 = new StringBuilder(); System.out.println(sb1); // (blank) // Using band structure StringBuilder sb2 = new StringBuilder("itcast"); System.out.println(sb2); // itcast }
common method
There are two common methods for StringBuilder:
public StringBuilder append(… ): adds a string form of any type of data and returns the current object itself.
public String toString(): converts the current StringBuilder object to a String object.
append method
The append method has a variety of overload forms and can receive any type of parameters. Any data as a parameter adds the corresponding string content to StringBuilder. For example:
public static void main(String[] args) { StringBuilder builder = new StringBuilder(); StringBuilder builder2 = builder.append("hello"); //Compare System.out.println("builder:"+builder); System.out.println("builder2:"+builder2); System.out.println(builder == builder2); //true // Any type can be added builder.append("hello"); builder.append("world"); builder.append(true); builder.append(100); // In our development, we will encounter the situation of returning an object after calling a method. Then use the returned object to continue calling the method. // In this case, we can chain the code together, like the append method. The code is as follows // Chain programming builder.append("hello").append("world").append(true).append(100); System.out.println("builder:"+builder); }
Note: StringBuilder has overridden and rewritten toString method in Object.
@Override public String toString() { // Create a copy, don't share the array return new String(value, 0, count); }
toString method
With the toString method, StringBuilder objects are converted to immutable String objects. Such as:
public static void main(String[] args) { // Chain creation StringBuilder sb = new StringBuilder("Hello").append("World").append("Java"); // Calling method String str = sb.toString(); System.out.println(str); // HelloWorldJava }