Easy Tutorial
❮ Android Tutorial Eclipse Adt Sdk App Getting Started With Typescript ❯

Differences Between Java String, StringBuffer, and StringBuilder

Category Programming Techniques

String

String: A string constant with an immutable length. In Java, a String is immutable.

The String class includes the following definitions:

/** The value is used for character storage. */
private final char value[];

/** The offset is the first index of the storage that is used. */
private final int offset;

/** The count is the number of characters in the String. */
private final int count;

The array used to store characters is declared as final, so it can only be assigned once and cannot be changed again.

StringBuffer (JDK1.0)

StringBuffer: A string variable (Synchronized, i.e., thread-safe). If you frequently modify the content of a string, it is more efficient to use StringBuffer. If you want to convert it to a String type, you can call the toString() method of StringBuffer.

Java.lang.StringBuffer is a thread-safe, mutable sequence of characters. At any given time, it contains a specific sequence of characters, but the length and content of the sequence can be changed through certain method calls. A string buffer can be safely used in multiple threads.

The main operations on StringBuffer are append and insert methods, which can be overloaded to accept any type of data. Each method effectively converts the given data into a string, then appends or inserts the characters of that string into the string buffer.

For example, if z refers to a string buffer object with the current content of start, then the method call z.append("le") will make the string buffer contain startle, while z.insert(4, "le") will change the string buffer to contain starlet.

StringBuilder (JDK5.0)

StringBuilder: A string variable (not thread-safe). Internally, a StringBuilder object is treated as a variable-length array containing a sequence of characters.

java.lang.StringBuilder is a mutable sequence of characters, which was newly added in JDK5.0. This class provides an API compatible with StringBuffer but does not guarantee synchronization. It is designed as a simple replacement for StringBuffer when the string buffer is used by a single thread (which is very common).

Its constructors are as follows:

Constructor Description
StringBuilder() Creates a StringBuilder object with a capacity of 16 (16 empty elements)
StringBuilder(CharSequence cs) Creates a StringBuilder object containing cs, with 16 empty elements appended at the end
StringBuilder(int initCapacity) Creates a StringBuilder object with a capacity of initCapacity
StringBuilder(String s) Creates a StringBuilder object containing s, with 16 empty elements appended at the end

In most cases, StringBuilder > StringBuffer. This is mainly because the former does not need to consider thread safety.

Differences Among the Three

The main performance difference between the String type and StringBuffer: String is an immutable object, so every time a change is made to the String type, a new String object is generated, and then the pointer points to the new String object. Therefore, it is best not to use String for strings that frequently change content, as each object generation will affect system performance, especially when there are many unreferenced objects in memory, and the JVM's GC will start working, reducing performance.

When using the StringBuffer class, operations are performed directly on the StringBuffer object itself, rather than generating a new object and changing the object reference. Therefore, StringBuffer is recommended in most cases, especially when the string object is frequently changed.

In some special cases, the string concatenation of String objects is actually compiled into the concatenation of StringBuffer objects by the Java Compiler, so the speed of String objects is not slower than that of StringBuffer objects in these cases, for example:

String s1 = “This is only a” + “ simple” + “ test”;
StringBuffer Sb = new StringBuilder(“This is only a”).append(“ simple”).append(“ test”);

The speed of generating the String s1 object is not slower than that of StringBuffer. In fact, the Java Compiler automatically made the following conversion:

The Java Compiler directly compiles the first statement above as:

String s1 = “This is only a simple test”;

So the speed is very fast. However, it should be noted that if the concatenated strings come from other String objects, the Java Compiler will not automatically convert, and the speed will not be as fast, for example:

``` String s2 = “This is only a”;
String s3 = “ simple”;
String s4

❮ Android Tutorial Eclipse Adt Sdk App Getting Started With Typescript ❯