Three brothers used to process strings in Java"
In Java, there are three common classes used to process strings: Java. lang. string, Java. lang. stringbuffer, Java. lang. stringbuilder, all of which share the final class and cannot be inherited. This is mainly based on performance and security, because these classes are often used, in addition, to prevent the modification of parameters from affecting other applications. The stringbuffer and stringbuilder are basically the same, but the stringbuffer is thread-safe and can be used in multiple threads without additional synchronization. The stringbuilder is non-synchronous and must be used for separate synchronization when running in multiple threads, however, the speed is much faster than stringbuffer. the similarities between the two can be achieved through append and insert for string operations.
String implements three interfaces: serializable, comparable <string>, and charsequence, while stringbuffer and stringbuilder only implement two interfaces: serializable and charsequence, in contrast, the string instance can be compared using the compareto method, but not the other two.
The following is a detailed introduction to string, stringbuffer, and stringbuilder in JDK:
String:
String
Class represents a string. Java
All character strings in the program (such as "ABC"
.
Strings are constants, and their values cannot be changed after creation. The string buffer supports variable strings. Because string
Objects are unchangeable, so they can be shared. For example:
String STR = "ABC ";
It is equivalent:
Char data [] = {'A', 'B', 'C '};
String STR = new string (data );
The following provides more examples of how to use strings:
System. Out. println ("ABC ");
String CDE = "CDE ";
System. Out. println ("ABC" + CDE );
String c = "ABC". substring (2, 3 );
String d = CDE. substring (1, 2 );
String
Classes can be used to check a single character, compare string, search string, extract substring, create a copy of string, and convert all characters to uppercase or lowercase letters. Case-sensitive ter ing based on character
Class specified Unicode
Standard Edition.
Java
The language provides special support for String concatenation symbols ("+") and conversion of other objects into strings. String concatenation is through
Stringbuilder (or stringbuffer) class and its append
Method. String Conversion is performed through tostring
Method.
Class, and can be
All classes in. For more information about String concatenation and conversion, see Gosling, joy
And Steele
The Java language specification.
Unless otherwise specified, null
The constructor or method passed to this class will throw nullpointerexception.
String
Represents a UTF-16
String in the format
By proxy
(For more information, see character
Class Unicode
Character representation ). Index value refers to Char
Code unit, so add characters in the string
Which occupies two locations.
String
Class provides Unicode Processing
Code Point (character) and Unicode
Code unit (that is, char
Value.
Stringbuffer:
A variable string of thread-safe characters. A
String
But 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 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.
Stringbuffer
The main operation on is append.
And insert
Methods to accept any type of data. Each method can effectively convert the given data into a string, and then add or insert the character of the string into the string buffer. Append
The method always adds these characters to the end of the buffer; while the insert
The method adds characters to the specified vertex.
For example, if
Reference a string buffer object whose current content is "start", this method calls Z. append ("Le ")
This causes the string buffer to contain "startle", while Z. insert (4, "Le ")
Changes the string buffer to include "starlet ".
Generally, if sb
Reference stringbuilder
For an instance, then sb. append (X)
And sb. insert (sb. Length (), X)
With the same effect.
If an operation is performed on the source sequence (such as adding or inserting a sequence in the source sequence), the operation is only performed on the string buffer for this operation, rather than on the source.
Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. From JDK
At the beginning of 5, a equivalence class used by a single thread is added for this class, that is
Stringbuilder. Compared with this class, it is usually preferred to use
Stringbuilder class, because it supports all the same operations, but because it does not execute synchronization, It is faster.
Stringbuilder:
A variable character sequence. This class provides
Compatible APIs, but cannot be synchronized. This class is designed as stringbuffer
Is 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 better than stringbuffer.
Fast.
This article is from:Feng Libin's blog