Java Notes

StringBuffer and StringBuilder

StringBuffer is used to store character strings that will be changed (String objects cannot be changed). It automatically expands as needed. Related classes: String, CharSequence.

StringBuilder was added in Java 5. It is identical in all respects to StringBuffer except that it is not synchronized, which means that if multiple threads are accessing it at the same time, there could be trouble. For single-threaded programs, the most common case, avoiding the overhead of synchronization makes the StringBuilder very slightly faster.

No imports are necessary because these are both in the java.lang package.

StringBuffer and StringBuilder methods and constuctors

Assume

StringBuffer sb = new StringBuffer();
StringBuffer sb2;
int i, offset, len;
char c;
String s;
char chararr[];
Constructors
sb = new StringBuffer(); Creates new, empty, StringBuffer
sb = new StringBuffer(n); Creates new StringBuffer of size n
sb = new StringBuffer(s); Creates new StringBuffer with initial value s
Building StringBuffer
sb2 = sb.append(x) appends x (any primitive or object type) to end of sb.
sb2 = sb.append(chararr, offset, len) appends len chars from chararr starting at index offset.
sb2 = sb.insert(offset, x) inserts x (char, int, String, ...) at position offset.
sb.setCharAt(index, c) replaces char at index with c
Deleting from StringBuffer
sb2 = sb.delete(beg, end) deletes chars at index beg thru end.
sb.setLength(n) Sets the length of the content to n by either truncating current content or extending it with the null character ('\u0000'). Use sb.setLength(0); to clear a string buffer.
Extracting from StringBuffer
c = sb.charAt(i) char at position i.
s = sb.substring(start) substring from position start to end of string.
s = sb.substring(startend) substring from position start to the char before end.
s = sb.toString() Returns String.
Searching
i = sb.indexOf(s) Returns position of first (leftmost) occurrence of s in sb.
i = sb.lastIndexOf(s) Returns position of last (rightmost) occurrence of s in sb.
Misc
i = sb.length() length of the string s.
sb2 = sb.reverse() reverses the contents.

Converting values

An interesting aspect of the append() and insert() methods is that the parameter may be of any type. These methods are overloaded and will perform the default conversion for all primitive types and will call the toString() method for all objects.

Chaining calls

Some StringBuffer methods return a StringBuffer value (eg, append(), insert(), ...). In fact, they return the same StringBuffer that was used in the call. This allows chaining of calls. Eg,

sb.append("x = ").append(x).append(", y = ").append(y);

Efficiency of StringBuffer compared to String

Because a StringBuffer object is mutable (it can be changed), there is no need to allocate a new object when modifications are desired. For example, consider a method which duplicates strings the requested number of times.

// Inefficient version using String.
public static String dupl(String s, int times) {
    String result = s;
    for (int i=1; i<times; i++) {
        result = result + s;
    }
    return result;
}

If called to duplicate a string 100 times, it would build 99 new String objects, 98 of which it would immediately throw away! Creating new objects is not efficient. A better solution is to use StringBuffer.

// More efficient version using StringBuffer.
public static String dupl(String s, int times) {
    StringBuffer result = new StringBuffer(s);
    for (int i=1; i<times; i++) {
        result.append(s);
    }
    return result.toString();
}

This creates only two new objects, the StringBuffer and the final String that is returned. StringBuffer will automatically expand as needed. These expansions are costly however, so it would be better to create the StringBuffer the correct size from the start.

// Much more efficient version using StringBuffer.
public static String dupl(String s, int times) {
    StringBuffer result = new StringBuffer(s.length() * times);
    for (int i=0; i<times; i++) {
        result.append(s);
    }
    return result.toString();
}

Because this StringBuffer is created with the correct capacity, it will never have to expand. There is no constructor which allows both an initial capacity to be specifed and an initial string value. Therefore the loop has one extra iteration in it to give the correct number of repetitions.