String Buffer: Difference between revisions

Content deleted Content added
m r2.7.3) (Robot: Removing ja:StringBufferとStringBuilder
Cewbot (talk | contribs)
Tag: Redirect target changed
 
(13 intermediate revisions by 11 users not shown)
Line 1:
#REDIRECT [[String (computer science)#String Buffers]]
In [[object-oriented programming]], a '''String Buffer''' is an alternative to a String. It has the ability to be altered through adding or appending, whereas a String is normally fixed or [[Immutable object|immutable]].
 
{{Redirect category shell|1=
== In Java ==
{{R from Merge}}
=== Theory ===
{{R to Section}}
[[Java (Sun)|Java]]'s standard way to handle text is to use its <code>{{Javadoc:SE|java/lang|String}}</code> [[Class (computer science)|class]]. Any given <code>String</code> in Java is an [[immutable object]], which means its [[State (computer science)|state]] cannot be changed. A <code>String</code> has an [[Array data type|array]] of [[Character (computing)|characters]]. Whenever a <code>String</code> must be manipulated, any changes require the creation of a new <code>String</code> (which, turn, involves the creation of a new array of characters, and copying of the original array). This happens even if the original <code>String</code>'s value or intermediate <code>String</code>s used for the manipulation are not kept.
}}
 
Java provides an alternate class for string manipulation, called a <code>'''[http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html StringBuffer]'''</code>. A <code>StringBuffer</code>, like a <code>String</code>, 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
<source lang="java">
String newString = aString + anInt + aChar + aDouble;
</source>
like this:
<source lang="java">
String newString = (new StringBuffer(aString)).append(anInt).append(aChar).append(aDouble).toString();
</source>
 
=== Implications ===
Generally, a <code>StringBuffer</code> is more [[analysis of algorithms|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 <code>StringBuffer</code>. In order to insert or remove characters at arbitrary positions, whole sections of arrays must be moved.
 
The method by which a <code>StringBuffer</code> 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.
 
The '''{{Javadoc:SE|java/lang|StringBuilder}}''' class, introduced in [[Java Platform, Standard Edition|J2SE]] 5.0, differs from <code>StringBuffer</code> in that it is [[Synchronization (computer science)|unsynchronized]]. When only a single [[Thread (computer science)|thread]] at a time will access the object, using a <code>StringBuilder</code> processes more efficiently than using a <code>StringBuffer</code>.
 
<code>StringBuffer</code> and <code>StringBuilder</code> are included in the {{Javadoc:SE|package=java.lang|java/lang}} package.
 
== In .NET ==
Microsoft's [[.NET Framework]] has a <code>StringBuilder</code> class in its [[Base Class Library]].
 
== In other languages ==
* In [[C++]] and [[Ruby (programming language)|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 (API)|Cocoa]]/[[OpenStep]] frameworks), the <code>NSMutableString</code> class is the mutable version of the <code>NSString</code> class.
 
== See also ==
* [[Analysis of algorithms]]
 
== Links ==
* The JavaDocs of <code>[http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html StringBuffer]</code>, <code>[http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html StringBuilder]</code> and <code>[http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html String]</code>.
* [http://www.java2s.com/Code/JavaAPI/java.lang/StringBuffer.htm The source code of these classes]
* [http://www-128.ibm.com/developerworks/java/library/j-jtp04223.html?ca=dgr-lnxw01JavaUrbanLegends Urban Performance Legends] - An article which involves a discussion of immutable objects with respect to [[object-oriented design]]
 
[[Category:Java programming language]]
[[Category:String data structures|Buffer]]
 
[[es:Búfer de cadena]]