||This article was nominated for deletion. The debate was closed on 04 June 2013 with a consensus to merge the content into the article String (computer science). If you find that such action has not been taken promptly, please consider assisting in the merger instead of re-nominating the article for deletion. To discuss the merger, please use the destination article's talk page. (June 2013)|
Java's standard way to handle text is to use its
class. Any given
String in Java is an immutable object, which means its state cannot be changed. A
String has an array of characters. Whenever a
String must be manipulated, any changes require the creation of a new
String (which, in turn, involves the creation of a new array of characters, and copying of the original array). This happens even if the original
String's value or intermediate
Strings used for the manipulation are not kept.
Java provides an alternate class for string manipulation, called a
StringBuffer, like a
String, has an array to hold characters. It, however, is mutable (its state can be altered). Its array of characters is not necessarily completely filled (as oppose to a String, whose array is always the exact required length for its contents). Thus, it has the capability to add, remove, or change its state without creating a new object (and without the creation of a new array, and array copying). The exception to this is when its array is no longer of suitable length to hold its content. In this case, it is required to create a new array, and copy contents.
For these reasons, Java would handle an expression like
String newString = aString + anInt + aChar + aDouble;
String newString = (new StringBuffer(aString)).append(anInt).append(aChar).append(aDouble).toString();
StringBuffer is more efficient than a String in string handling. However, this is not necessarily the case, since a StringBuffer will be required to recreate its character array when it runs out of space. Theoretically, this is possible to happen the same number of times as a new String would be required, although this is unlikely (and the programmer can provide length hints to prevent this). Either way, the effect is not noticeable in modern desktop computers.
As well, the shortcomings of arrays are inherent in a
StringBuffer. In order to insert or remove characters at arbitrary positions, whole sections of arrays must be moved.
The method by which a
StringBuffer is attractive in an environment with low processing power takes this ability by using too much memory, which is likely also at a premium in this environment. This point, however, is trivial, considering the space required for creating many instances of Strings in order to process them. As well, the StringBuffer can be optimized to "waste" as little memory as possible.
StringBuilder class, introduced in J2SE 5.0, differs from
StringBuffer in that it is unsynchronized. When only a single thread at a time will access the object, using a
StringBuilder processes more efficiently than using a
StringBuilder are included in the
In other languages
- In C++ and Ruby, the standard string class is already mutable, with the ability to change the contents and append strings, etc., so a separate mutable string class is unnecessary.
- In Objective-C (Cocoa/OpenStep frameworks), the
NSMutableStringclass is the mutable version of the