We know that concat method or the concatenation operator +, can be used to concatenate two strings of String class. Concatenation of strings of StringBuffer class is done using the append method of StringBuffer class. This method is overloaded to take all types of arguments. append method exists in the following forms:
public StringBuffer append(Object obj)
public StringBuffer append(String str)
public StringBuffer append(StringBuffer sb)
public StringBuffer append(CharSequence s)
public StringBuffer append(CharSequence s, int start, int end)
public StringBuffer append(char[] str)
public StringBuffer append(char[] str, int offset, int len)
public StringBuffer append(boolean b)
public StringBuffer append(char c)
public StringBuffer append(int i)
public StringBuffer append(long lng)
public StringBuffer append(float f)
public StringBuffer append(double d)

As can be noticed, the append method returns StringBuffer object which is nothing but the object with which the method is called.
Let us look at these methods in detail through the following program :

Appending another StringBuffer- abcdefAppending a String- abcdefghiAppending a character array- abcdefghijklAppending part of a character array- abcdefghijkljkAppending a boolean- abcdefghijkljktrueAppending a character- abcdefghijkljktrueZAppending an integer- abcdefghijkljktrueZ1Appending a double- abcdefghijkljktrueZ12.35Appending an object- abcdefghijkljktrueZ12.35StringBufferAppend@19821f

DESCRIPTION

This example shows how to append String, StringBuffer, char[], int, double, char, boolean to a String Buffer. They are converted to string and added as they are, at the end of the String. Look atLINE A, here only a part of the character array c1, is appended to the string of StringBuffer class. The first argument is the character array, the second argument is the starting index of the character array from which the characters are to be appended and the third argument is the number of characters that are to be appended from the starting index. In LINE C, we are trying to append object of a class to the string. Here the string form of object is appended. String form is obtained from the toString method which is called internally. Note that the value after @ might be different for every run instead of 19821f.

THINGS TO TRY

Pass null value as argument to the append method and observe. We get a compile time error because there arises an ambiguity since null can be taken by many overloaded forms like append(String s), append(Object o), append(StringBuffer sb), append(CharacterSequence cs) and hence compiler does not understand which of these versions to consider.

Replace LINE B with StringBufferAppend sba = null; and try to append sba to a1. Here a1 is appended with the four characters of null. Observe the difference between this point and the previous point. Previously, we are trying to append null directly. But now we are appending an object of a class which holds null reference. In this case no ambiguity arises and the respective overloaded version, append(Object o) will be considered.