1. The content of the String object cannot be changed, and the StringBuffer can be changed and efficient;
2. String
String a = "123 ";
A = a + "456 ";
In the above two sentences, although the value of a is eventually changed, in fact, during compilation, the first sentence a is an object, assigned an address, and the second sentence is executed, the original a is released and then reassigned.
A originally points to a String object instance ("123"). a + "456" creates another String object instance ("123456 "), then a points to the new String instance.
3. StringBuffer
StringBuffer B = new StringBuffer ("asd ");
B. append ("fgh ");
In this process, only one object such as B exists, and B always points to a StringBuffer instance. append, which only changes the content of this instance.
Take a look at the following code:
Repeat 26 English letters for 5000 times,
String tempstr = "abcdefghijklmnopqrstuvwxyz ";
Int times = 5000;
Long lstart1 = System. currentTimeMillis ();
String str = "";
For (int I = 0; I <times; I ++)
{
Str + = tempstr;
}
Long lend1 = System. currentTimeMillis ();
Long time = (lend1-lstart1 );
System. out. println (time );
Unfortunately, my computer is not a supercomputer, and the result is usually about 154735.
That is, 154 seconds.
Let's take a look at the following code:
String tempstr = "abcdefghijklmnopqrstuvwxyz ";
Int times = 5000;
Long lstart2 = System. currentTimeMillis ();
StringBuffer sb = new StringBuffer ();
For (int I = 0; I <times; I ++)
{
Sb. append (tempstr );
}
Long lend2 = System. currentTimeMillis ();
Long time2 = (lend2-lstart2 );
System. out. println (time2 );
The result is 16. Sometimes it is 0.
Therefore, the conclusion is obvious that the StringBuffer speed is almost ten thousand times faster than that of String. Of course, this data is not very accurate. Because the number of cycles is 100000, the difference is greater. Try it.
Next I will explain why StringBuffer is much more efficient than String.
What is the difference between String and StringBuffer?
If you search online, there will be a lot of answers, the most of which is that String is immutable and StringBuffer is variable, but what does this mean? If you can debug with IDE, you will find that after String is instantiated, all attributes are final, and StringBuffer is indeed not. This is variable and immutable. The following is an example of SCJP:
Java code
Public class Test {
Public static void stringReplace (String text ){
Text = text. replace (j, I );
}
Public static void bufferReplace (StringBuffer text ){
Text = text. append ("C ");
}
Public static void main (String args []) {
String textString = new String ("java ");
StringBuffer textBuffer = new StringBuffer ("java ");
StringReplace (textString );
BufferReplace (textBuffer );
System. out. println (textString + textBuffer );
}
}
The answer is javajavaC.
This is because the append method changes the value of text in the seventh line of text = text. append ("C"), and the address pointed by text and textBuffer is the same. Therefore, javaC is printed.
Another example:
String a = "a"; // assume a points to the address 0x0001.
A = "B"; // After the value is re-assigned, a points to address 0x0002, but the "a" saved in address 0x0001 still exists, but it is no longer pointed to by.
Therefore, the String operation changes the value assignment address instead of the value change operation.
The length of a String object is fixed and cannot be changed, or a new character is appended to the String object. You may use + to concatenate strings to append new characters or strings, but + will generate a new String instance. If the program requires this type of additional string frequently, we do not recommend that you use + to concatenate strings. In object-oriented programming, it is best to reuse the generated objects. The generation of objects requires memory space and time. Continuously generating String instances is inefficient.
J2SE 5.0 provides the java. lang. StringBuilder class. By default, the object generated by this class has a length of 16 characters. You can also specify the initial length. If the length of the attached characters exceeds the maximum length, the StringBuilder object will automatically increase the length to accommodate the appended characters. If you need to frequently append strings, using StringBuilder will greatly improve the program efficiency. Through the following simple test program, we can know the difference in performance.
Ü example 6.5 AppendStringTest. java
Public class AppendStringTest {
Public static void main (String [] args ){
String text = "";
Long beginTime = System. currentTimeMillis ();
For (int I = 0; I <10000; I ++)
Text = text + I;
Long endTime = System. currentTimeMillis ();
System. out. println ("execution time:" + (endTime-beginTime ));
StringBuilder builder = new StringBuilder ("");
BeginTime = System. currentTimeMillis ();
For (int I = 0; I <10000; I ++)
Builder. append (String. valueOf (I ));
EndTime = System. currentTimeMillis ();
System. out. println ("execution time:" + (endTime-beginTime ));
}
}
In Example 6.5, we first use + to concatenate strings, and use System. currentTimeMillis () to obtain the System time before and after the for loop execution, so that we can know how long the for loop has been executed. Here are the test data on my computer:
Execution time: 4641
Execution time: 16
We can see that the execution time is very different, which shows the burden of using the + concatenation string. If you need to append strings frequently, StringBuilder is recommended. In fact, for example 6.5, the execution time of the second for loop can be shorter, because append () can also accept the basic data type, so you do not need to use String specially. the valueOf () method retrieves a String from the int. The execution time can be greatly shortened as follows:
For (int I = 0; I <10000; I ++)
Builder. append (I );
Use StringBuilder to output the string result, you can use the toString () method. You can use the length () method to learn the character length of the current object, and capacity () can return the size of the characters that the object Currently can accommodate. In addition, StringBuilder also has the insert () method to insert a character into a specified position. If there is a character after this position, all the characters will be moved back; deleteChar () the reserve () method can delete characters at the specified position, while the reserve () method can reverse the string. For detailed usage, You can query the API file description of java. lang. StringBuilder.
StringBuilder is a class added only when J2SE 5.0 is used. java. lang. StringBuffer is used if the same version before J2SE 5.0 has the same requirements. In fact, StringBuilder is designed to have the same operation interface as StringBuffer. Using StringBuilder with a single machine that does not have multiple threads makes it more efficient because StringBuilder does not handle synchronization issues. StringBuffer will solve the synchronization problem. If StringBuilder is operated under multiple threads, use StringBuffer to manage the synchronization problem on its own.