StringBuilder .append() Method Tutorial

In this tutorial I will talk about the append() method. The append method simply appends the value of the parameter to the value of the current StringBuilder object. There are more than a dozen overloaded versions of the append() method that will allow you to pass in quite a few data types. The most popular version of the append() method is the one with a String parameter. That version of the append() method works like this:
StringBuilder sb = new StringBuilder("Apples ");
sb.append("& Oranges");
System.out.println(sb);
// Apples & Oranges

Notice that the append method changed the value (state) of the StringBuilder object from "Apples " to "Apples & Oranges" without re-assigning the sb reference variable to a new object. This is because StringBuilder objects are mutable – meaning that the value of a StringBuilder object can be changed.
The following code demonstrates the notable difference between String objects and StringBuilder objects:
String s = new String("Hello");
StringBuilder sb = new StringBuilder("Hello");
s.concat(" World");
sb.append(" World");
System.out.println("s = " + s);
// s = Hello
System.out.println("sb = " + sb);
// sb = Hello World

If you are unfamiliar with the difference between mutable and immutable objects, please watch my StringBuilder Introduction Tutorial.

Here is a list of the various overloaded versions of the append method:

  • append(boolean b)
  • append(char c)
  • append(char[] str)
  • append(char[] str, int offset, int len)
  • append(CharSequence s)
  • append(CharSequence s, int start, int end)
  • append(double d)
  • append(float f)
  • append(int i)
  • append(long lng)
  • append(Object obj)
  • append(String str)
  • append(StringBuffer sb)



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>
C:\Java>md StringBuilderAppend
C:\Java>cd StringBuilderAppend
C:\Java\StringBuilderAppend>Notepad StringBuilderAppend.java

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


class StringBuilderAppend {
    public static void main(String args[]) {
        boolean b = true;
        char c = ' ';
        char cPeriod[] = { '.', '\n' } ;
        char cQMark[] = { '?', '\n' };
        CharSequence cs1 = new String("He responds $");
        CharSequence cs2 = new String("????He hands the bellhop ????");
        double d = 30.00;
        float f = 27.00f;
        int i = 0;
        long L = 5;
        Object o1 = new String("Three traveling salesmen stop at a motel to spend the night");
        Object o2 = new String(" dollar bills and tells him to give the salesmen their refund");
        String s = "They ask the desk clerk how much for a room";
        StringBuffer sBuff = new StringBuffer("When the bellhop returns to the main desk, \nthe clerk informs the bellhop that the ");
        CharSequence cs3 = new StringBuilder("On the way back to the room, the bellhop thinks to himself ...\n");
        Object o3 = new StringBuilder("3 guys, 5 dollars - I'll pocket $2 and give them each a dollar back");
        CharSequence cs4 = new StringBuffer("He knocks on the door and hands them each a dollar back. \nEnd of story right");
        Object o4 = new StringBuffer("The bellhop kept $2.00, the room was originally $30.00");
	
        
        StringBuilder sb = new StringBuilder();
        sb.append(o1);
        sb.append(cPeriod);
        sb.append(s);
        sb.append(cQMark);
        sb.append(cs1);
        sb.append(d).append(i).append(cPeriod);
        sb.append("Each salesman pays $10 and the bellhop shows them to their room.\n");
        sb.append(sBuff);	
        sb.append(b);
        sb.append(" room rate should have been $25.00");
        sb.append(cPeriod);
        sb.append(cs2, 4, 25);
        sb.append(L);
        sb.append(o2);
        sb.append(cPeriod);
        sb.append(cs3);
        sb.append(o3);
        sb.append(cPeriod);
        sb.append(cs4);
        sb.append(cQMark);
        sb.append("At this point each of the salesmen paid $9.00 for the room, so $9.00 * 3 = $");
        sb.append(f).append(i).append(cPeriod);
        sb.append(o4).append(cPeriod);
        sb.append("Where did the other dollar go").append(cQMark);
        System.out.println(sb);
    }
}    


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


C:\Java\StringBuilderAppend>javac StringBuilderAppend.java
C:\Java\StringBuilderAppend>java StringBuilderAppend
Three traveling salesmen stop at a motel to spend the night. 
They ask the desk clerk how much for a room? 
He responds $30.00. 
They each slap down $10 and the bellhop takes them up to their room.
When the bellhop comes back to the main desk, 
the clerk informs the bellhop that the true room rate should have been $25.00.
He hands the bellhop 5 one dollar bills and tells him to give the salesmen their refund.
On the way back to the room, the bellhop thinks to himself ... 
3 guys, 5 dollars - I'll pocket $2 and give them each a dollar back.
He knocks on the door and hands them each a dollar back. 
End of story right?
At this point each of the salesmen paid $9.00 for the room, so $9.00 * 3 = $27.00. 
The bellhop kept $2.00, the room was originally $30.00. 
Where did the other dollar go?


Final thoughts

When you have a method like the append method with so many overloaded versions, they tend to become gold mines for converting between data types. Let's say you want to convert a primitive double to a String. There are many ways we could do this, but we know we can pass a double argument to the StringBuilder append method. From there we know that every class has inherited the toString() method from the Object class. Bingo ... instant double to String conversion tool! String s = new StringBuilder().append(389.75).toString();


Tutorials