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 :
append method of StringBuffer
class StringBufferAppend
{
public static void main(String arg[])
{
StringBuffer a1 = new StringBuffer("abc");
StringBuffer a2 = new StringBuffer("def");
System.out.println("Appending another StringBuffer- "+ a1.append(a2));
String a3 = "ghi";
System.out.println("Appending a String- "+ a1.append(a3));
char c1[] = {'j','k','l'};
System.out.println("Appending a character array- "+ a1.append(c1));
System.out.println("Appending part of a character array- "+ a1.append(c1,0,2)); //LINE A
System.out.println("Appending a boolean- "+ a1.append(true));
System.out.println("Appending a character- "+ a1.append('Z'));
System.out.println("Appending an integer- "+ a1.append(1));
System.out.println("Appending a double- "+ a1.append(2.35));
StringBufferAppend sba = new StringBufferAppend(); //LINE B
System.out.println("Appending an object- "+ a1.append(sba)); //LINE C
}
}
OUTPUTAppending another StringBuffer- abcdef
Appending a String- abcdefghi
Appending a character array- abcdefghijkl
Appending part of a character array- abcdefghijkljk
Appending a boolean- abcdefghijkljktrue
Appending a character- abcdefghijkljktrueZ
Appending an integer- abcdefghijkljktrueZ1
Appending a double- abcdefghijkljktrueZ12.35
Appending an object- abcdefghijkljktrueZ12.35StringBufferAppend@19821f
DESCRIPTIONThis 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.