StringBuilder Introduction Tutorial

I am going to produce several tutorials relating to the StringBuilder class. In each of these tutorials I will be comparing similarities and differences to the String class. The String class has all sorts of nuances that I have addressed in my multi-part String tutorials. I recommend watching all of the String related tutorials to get a good feel for the String class capabilities. The most notable difference between the StringBuilder class and the String class is that String class is immutable and the StringBuilder class is mutable.

Object state is a fancy term for describing the values held by instance variables. Immutable means that the object state cannot be modified once it is instantiated. Mutable means that the object state can be modified after it is instantiated.

Immutable state example:
final char objectState[] = { 'H', 'E', 'L', 'L', 'O' };
objectState = new char[] { 'C', 'A', 'T'};
// Illegal! final won't allow objectState to be assigned to another object

Mutable state example:
char objectState[] = { 'H', 'E', 'L', 'L', 'O' };
objectState = new char[] { 'C', 'A', 'T'};
// no problem - object state is now CAT

There are several constructors in the StringBuilder class.
The first constructor is a no-argument constructor that creates a StringBuilder object with an initial capacity of 16 characters. It is important to note that capacity and length are two completely separate things.
StringBuilder s1 = new StringBuilder();
System.out.println(s1.length()); // 0
System.out.println(s1.capacity()); // 16
The length() method will return the actual length of the value contained in the object state. The capacity() method will return the size of the internal available character sequence size. Fortunately you do not have worry about maintaining the size of the character sequence for your object state. The JVM will take care of that work.

Optionally, you can set the initial capacity of the StringBuilder character sequence by using another constructor that takes a primitive int argument.
StringBuilder s2 = new StringBuilder(300);
System.out.println(s2.length()); // 0
System.out.println(s2.capacity()); // 300

Another constructor simply takes a string argument.
StringBuilder s3 = new StringBuilder("Hello World");
System.out.println(s3.length()); // 11
System.out.println(s3.capacity()); // 27

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md StringBuilderIntro
C:\Java>cd StringBuilderIntro

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

class StringBuilderIntro {
    public static void main(String args[]) {
        StringBuilder s1 = new StringBuilder();
        System.out.println("s1 length: " + s1.length());
        System.out.println("s1 capacity: " + s1.capacity());

        StringBuilder s2 = new StringBuilder(300);
        System.out.println("s2 length: " + s2.length());
        System.out.println("s2 capacity: " + s2.capacity());

        StringBuilder s3 = new StringBuilder("Hello World");
        System.out.println("s3 length: " + s3.length());
        System.out.println("s3 capacity: " + s3.capacity());


        String s = new String("Hello");
        StringBuilder sb = new StringBuilder("Hello");

        s.concat(" World");
        sb.append(" World");

        System.out.println("s = " + s);	
        System.out.println("sb = " + sb);

Now switch back to the command prompt (CMD) and type in javac and press Enter.
Now type in java StringBuilderIntro and press Enter.

C:\Java\StringBuilderIntro>java StringBuilderIntro
s1 length: 0
s1 capacity: 16
s2 length: 0
s2 capacity: 300
s3 length: 11
s3 capacity: 27


s = Hello
sb = Hello World

Final thoughts

This tutorial just serves as an introduction to the StringBuilder class. Stay tuned for more StringBuilder tutorials where I will demonstrate many of the various methods in the StringBuilder class.