how2examples.com
Home - Introduction to Java Programming

Java's StringBuilder class

The java.lang.StringBuilder class is used to represent character strings that can be dynamically modified. This is required as String objects are immutable - they cannot have their value altered once created.

Constructing new StringBuilder objects

The StringBuilder class has the following constructors:

StringBuilder methods

Below is a description of some of the methods provided by StringBuilder:

Most of the methods of the StringBuilder class return a reference to themselves - allowing multiple operations on a StringBuilder instance to be chained together in a single statement. This technique is known as method chaining.

Use of method chaining with StringBuilder.
Code snippet:
StringBuilder sb = new StringBuilder("qwerty");
sb.reverse().append("abcdef").insert(8, "Z");
System.out.println(sb);
Output from running code snippet:
ytrewqabZcdef

Advantages of StringBuilder

When performing concatenation of lots of String objects (e.g. in a loop), a StringBuilder can provide better performance then using the + operator. A StringBuilder can be more efficient as it creates less objects.

Perfomance advantage of StringBuilder compared to String.
Code snippet:
long start = System.nanoTime();
String s = "";
for (int i=0; i<5000; i++) {
   s += i;
}
long duration = System.nanoTime()-start;
System.out.println(duration + " nanoseconds for String version");

start = System.nanoTime();
StringBuilder sb = new StringBuilder();
for (int i=0; i<5000; i++) {
   sb.append(i);
}
duration = System.nanoTime()-start;
System.out.println(duration + " nanoseconds for StringBuilder version");

start = System.nanoTime();
sb = new StringBuilder(18890);
for (int i=0; i<5000; i++) {
   sb.append(i);
}
duration = System.nanoTime()-start;
System.out.println(duration + " nanoseconds for StringBuilder with large initial capacity version");
Output from running code snippet:
308243046 nanoseconds for String version
972206 nanoseconds for StringBuilder version
581890 nanoseconds for StringBuilder with large initial capacity version

The java.lang.StringBuffer class

Before StringBuilder was introduced in Java 5 the java.lang.StringBuffer class was the standard way of working with modifiable sequences of characters. Both StringBuffer and StringBuilder extend java.lang.AbstractStringBuilder. The significant difference between the two classes is that StringBuffer is synchronised while StringBuilder is not. The lack of syncronisation makes StringBuilder faster and therefore the recommended choice for single-threaded use. If the object is to be used by multiple threades then StringBuffer is the recommended option.

Example of using StringBuffer - note it is used in the same way as StringBuilder.
Code snippet:
StringBuffer sb = new StringBuffer("qwertyuiop");
sb.append("qwertyuiop");
sb.append(' ');
sb.append(1);
sb.append(false);
sb.append(2.35);
System.out.println(sb);
Output from running code snippet:
qwertyuiopqwertyuiop 1false2.35

See Also