StringBuilder and StringBuffer

Source: Internet
Author: User

Ext.: http://www.cnblogs.com/pepcod/archive/2013/02/16/2913557.html

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 category and are not allowed to be inherited, primarily in terms of performance and security, since these classes are often used and are considered to prevent the modification of the parameters in them from affecting 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 separate synchronization, but faster than stringbuffer, the common denominator between the two can be done by append, insert string operation.

A String implements three interfaces:Serializable, comparable<string>, Charsequence,

StringBuffer and StringBuilder only implemented two interfaces Serializable, charsequence, in contrast to the String instance can be 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. Make it 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.

StringBuilder and StringBuffer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.