Topics Index

Strings > StringBuffer >
Siva Nookala - 20 Feb 2017
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 :
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    

Appending 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


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.

  • 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.

Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.