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. So to be able to share. Like what:
String str = "abc"; equivalent to char data[] = {' A ', ' B ', ' C '}; string str = new string (data);
StringBuffer: String
The string buffer. But cannot be changed. Although it includes a specific sequence of characters at random points in time, some method calls can change the length and content of the sequence.
append
insert
Method. These methods can be overloaded to accept arbitrary types of data. Each method effectively converts the given data to a string. The character of the string is then appended or inserted into the string buffer. append
insert
Method adds a character to the specified point.
String buffers can be safely used with multiple threads. Be able to synchronize these methods when necessary. As a result, all operations on a 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 aStringBuffer
compatible APIs, but does not guarantee synchronization. This class is designed to be used asStringBuffer
A simple replacement for a string buffer that is used by a single thread (this is a very common situation).If possible, it is recommended that the class be preferred, since in most implementations it is moreStringBuffer
be quick. Span style= "FONT-FAMILY:ARIAL,HELVETICA,SANS-SERIF; Font-weight:normal "> Main operation is Append
and insert
method. These methods can be overloaded to accept arbitrary types of data. Each method effectively converts the given data to a string. The character of the string is then appended or inserted into the string generator. append
method always adds these characters to the end of the generator, while the insert
Method adds a character to the specified point.
2. Differences:
Comparison of operating speed: StringBuilder > stringbuffer.
Suppose 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
The difference between String,stringbuffer and StringBuilder