1. Definition:
String: String
class represents strings. All string literals (such as) in a Java program "abc"
are implemented as instances of this class. Strings are constants, and their values cannot be changed after they are created. A string buffer supports variable strings. Because the String object is immutable, it can be shared. For example:
String str = "abc"; equivalent to char data[] = {' A ', ' B ', ' C '}; string str = new string (data);
StringBuffer: String
The string buffer, but cannot be modified. Although it contains a specific sequence of characters at any point in time, some method calls can change the length and content of the sequence.
append
insert
method, which overloads these methods to accept arbitrary types of data. Each method effectively converts the given data into a string, and then appends or inserts the character of the string into the string buffer. append
insert
Method adds a character at the specified point.
String buffers can be safely used with multiple threads. These methods can be synchronized if necessary, so all operations on any particular instance appear to occur in a serial order that is consistent with the sequence of method calls made by each thread involved.
stringbuilder: A variable sequence of characters. This class provides an API that is compatible with stringbuffer
stringbuffer
stringbuffer
fast. /span> main operation is append
and insert
method, you can overload these methods to accept any type of data. Each method effectively converts the given data into a string, and then appends or inserts the character of the string into the string builder. append
method always adds these characters to the end of the generator, while the insert
Method adds a character at the specified point. 2. Difference:Comparison of execution speed: StringBuilder > stringbuffer;
If you want to manipulate a small amount of data with = String
single-threaded operation string buffer Large amount of data under operation = StringBuilder
Multi-threaded operation string buffers operation of large amounts of data = StringBuffer
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The difference between String,stringbuffer and StringBuilder