There are three classes commonly used in Java for working with strings: java.lang.String, Java.lang.StringBuffer, Java.lang.StringBuilder, all three are in the final class and are not allowed to be inherited, primarily in terms of performance and security, because these classes are often used, and take into account the fact that the parameters are prevented from being modified to affect other applications. StringBuffer and StringBuilder two are basically the same, just StringBuffer is thread-safe, can not require additional synchronization for multi-threading, StringBuilder is non-synchronous, running in multi-threading requires the use of a separate synchronization processing, But the speed is much faster than the stringbuffer; The common denominator between the two can be done by append, insert for string manipulation.
A String implements three interfaces: Serializable, comparable<string>, Charsequence, While StringBuffer and StringBuilder only implemented two interfaces serializable, charsequence, compared to string instances can be compared by CompareTo method, while the other two can not.
The following is a detailed description of the string, StringBuffer, StringBuilder in the JDK:
String:
The String class represents strings. All string literals (such as "ABC") in a Java program 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";
is equivalent to:
Char data[] = {' A ', ' B ', ' C '};
String str = new string (data);
Here are some 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);
The string class includes methods that can be used to examine a single character of a sequence, compare strings, search strings, extract substrings, create a copy of a string, and convert all characters to uppercase or lowercase. The case map is based on the Unicode standard version specified by the Character class.
The Java language provides special support for string concatenation symbols ("+") and for converting other objects to strings. string concatenation is implemented through the StringBuilder (or StringBuffer) class and its append methods. String conversions are implemented by the ToString method, which is defined by the Object class and can be inherited by all classes in Java. For more information about string concatenation and conversion, see the Java Language specification co-authored by Gosling, Joy, and Steele.
Unless otherwise noted, passing a null argument to a construction method or method in this class throws NullPointerException.
String represents a UTF-16-formatted string in which the supplementary characters are represented by surrogate pairs (for more information, see Unicode character representations in the Character Class). The index value refers to a char unit of code, so the supplementary character occupies two positions in the String.
The String class provides methods for processing Unicode code points (that is, characters) and Unicode code units (that is, char values).
StringBuffer:
A variable sequence of characters for thread safety. A string- like 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.
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.
The main operations on StringBuffer are the Append and insert methods, which can be overloaded to accept arbitrary types of data. Each method effectively converts the given data into a string, and then adds or inserts the character of the string into the string buffer. The Append method always adds these characters to the end of the buffer, while the Insert method adds characters at the specified point.
For example, if z refers to a string buffer object where the current content is "start", this method call Z.append ("le") causes the string buffer to contain "startle", and Z.insert (4, "le") will change the string buffer to contain " Starlet ".
Typically, if SB refers to an instance of StringBuilder, Sb.append (x) and Sb.insert (Sb.length (), x) have the same effect.
Whenever an operation occurs about a source sequence, such as an addition or insertion in a source sequence, the class only implements synchronization on the string buffer that performs this operation instead of on the source.
Each string buffer has a certain amount of capacity. As long as the string buffer contains a sequence of characters that does not exceed this capacity, there is no need to allocate a new array of internal buffers. If an internal buffer overflows, this capacity is automatically incremented. Starting with JDK 5, this class complements the equivalence class used by a single thread, StringBuilder. The StringBuilder class should usually be preferred over this class because it supports all the same operations, but it is faster because it does not perform synchronization.
StringBuilder:
A variable sequence of characters. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed to be used as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. If possible, it is recommended that this class be preferred because, in most implementations, it is faster than StringBuffer.
The "three brothers" in Java for working with strings