string aTest = "abc"; // allocate a fixed memory size aTest + = "ddd"; // allocate destroying the original data again, consumption StringBuilder sb = new StringBuilder (20) ; // specified allocation size, specify the allocation memory size, performance will be enhanced. // If the system exceeds the specified size, the system will be doubled, automatically increased, 40,60,80 sb.append ( "AAA"); // allocated to the heap area sb.Append ( "ddd"); // not destruction, added directly to the back
StringBuilder does not re-create a string object, if stringbuilder no pre-defined length, the default length of 16,
Greater than 16 and less than 32, automatically it reallocates 32, i.e. a multiple of 16.
Use StringBuilder needs to know in advance the length, to avoid wasting space.
to sum up:
After the string declaration memory size can not be amended, but are free to expand the size of the StringBuilder
string allocated to the stack area, stringbuilder allocated to the heap.
String modification more frequently using StringBuilder.
Reprinted from: https: //www.cnblogs.com/u3ddjw/p/6823346.html