The difference between string and stringbuffer can be said to be numerous online materials, but I can see this articleArticle, I feel that the small example is very representative, so I will take a look and make a summary myself.
There are three classes in Java to take charge of character operations.
1. character operates on a single character,
2. String operations on a string of characters. Immutable class.
3. stringbuffer also operates on a string of characters, but it can be a variable class.
String:
Yes. The object is not of the original type.
An unchangeable object. Once created, its value cannot be modified.
To modify an existing String object, create a new object and save the new value.
String is a final class, which cannot be inherited.
Stringbuffer:
It is a mutable object. objects are not re-created as strings when modified.
It can only be created by constructors,
Stringbuffer sb = new stringbuffer ();
Note: you cannot pay for a value by using a value symbol.
SB = "Welcome to here! "; // Error
After the object is created, the memory space is allocated in the memory, and a NULL is initially saved to stringbuffer.
You can use its append method to pay a value.
SB. append ("hello ");
Stringbuffer is more efficient than string in string connection operations:
String STR = new string ("Welcome ");
STR + = "here ";
In fact, by creating a stringbuffer, let Hou call append (), and finally
Then convert stringbuffer tosting ();
In this case, the string connection operation is more than the stringbuffer operation, of course, the efficiency is reduced.
Since the string object is an immutable object, each operation of sting creates a new object to save the new value.
In this way, the original object will be useless and it will be recycled. This will also affect the performance.
Take a look at the followingCode:
Repeat 26 English letters for 5000 times,
string tempstr = "abcdefghijklmnopqrstuvwxyz";
int times = 5000;
long lstart1 = system. currenttimemillis ();
string STR = "";
for (INT I = 0; I STR + = tempstr;
}< br> long lend1 = system. currenttimemillis ();
long time = (lend1-lstart1);
system. out. println (time);
unfortunately, my computer is not a supercomputer, and the result is not necessarily the same every time, generally around 46687.
that is, 46 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.
According to the above:
STR + = "here ";
In fact, by creating a stringbuffer, let Hou call append (), and finally
Then convert stringbuffer tosting ();
So STR + = "here"; can be equivalent
Stringbuffer sb = new stringbuffer (STR );
SB. append ("here ");
STR = sb. tostring ();
Therefore, the code that uses "+" to connect to a string is basically equivalent to the following code:
String tempstr = "abcdefghijklmnopqrstuvwxyz ";
Int times = 5000;
Long lstart2 = system. currenttimemillis ();
String STR = "";
For (INT I = 0; I <times; I ++ ){
Stringbuffer sb = new stringbuffer (STR );
SB. append (tempstr );
STR = sb. tostring ();
}
Long lend2 = system. currenttimemillis ();
Long time2 = (lend2-lstart2 );
System. Out. println (time2 );
The average execution time is about 46922, that is, 46 seconds.
Summary: IfProgramIf you need to frequently modify the connection operation on the string, the performance of using stringbuffer will be higher.