Difference between String, StringBuffer and StringBuilder, stringbuffer
1. Definition:
String:StringClass represents a string. All character strings in Java (such"abc". Strings are constants, and their values cannot be changed after creation. String Buffer supports variable character strings. Because the String object is unchangeable, it can be shared. For example:
String str = "abc"; equivalent to char data [] = {'A', 'B', 'C'}; String str = new String (data );
StringBuffer:A variable string of thread-safe characters. AStringBut cannot be modified. Although it contains a specific character sequence at any time point, the length and content of the sequence can be changed by calling some methods.
The main operation isappendAndinsertMethods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string into the string buffer.appendThe method always adds these characters to the end of the buffer.insertThe method adds characters to the specified vertex.
The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary, so all the operations on any specific instance are in serial order, this sequence is consistent with the method call sequence of each involved thread.
StringBuilder: a variable character sequence. This class providesStringBufferCompatible APIs, but cannot be synchronized. This class is designed for useStringBufferIs used when the string buffer is used by a single thread (this is common ). If possible, we recommend that you use this class first, because in most implementations, it is betterStringBufferFast. The main operation isappendAndinsertMethods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string to the string generator.appendThe method always adds these characters to the end of the generator.insertThe method adds characters to the specified vertex. 2. Differences:Comparison of execution speed: StringBuilder> StringBuffer;
If you want to operate on a small amount of data, use = String
A single thread operates on a large amount of data in the string buffer = StringBuilder
Multi-threaded operation of a large amount of data in the string buffer = StringBuffer
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.