StringBuffer class allows creating mutable string objects. One of the frequent operations on strings is adding a string or sequence of characters at the end of an existing string. Java Append Method in StringBuffer class does this. The Java append method is overloaded to append different kind of characters or data.
Before appending the argument value it is implicitly converted into the corresponding string representation.
Let’s discuss all these different variations of Java Append method in StringBuffer class with examples that you can copy and execute.
append(boolean)
This method, when called with a Boolean value, appends it at the end of the string value of a StringBuffer object. The Boolean value is first converted into its corresponding string representation. These Boolean values are True or false.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer(); boolean booVal=false; newStr.append(booVal); System.out.println("StringBuffer Object="+newStr); booVal=true; newStr.append(booVal); System.out.println("StringBuffer Object="+newStr); newStr.append(20==20); System.out.println("StringBuffer Object="+newStr); } }
Output
append(char c)
Append method, when called with a character value as argument, adds the corresponding string representation of the character at the end of the value of StringBuffer Object.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("This is Jav"); char charVal='a'; newStr.append(charVal); System.out.println("StringBuffer Object="+newStr); } }
Output
append(char[] str)
When a character array is passed as an argument to append method, it adds the string representation of the character array at the end of the StringBuffer Object.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are Learning"); char[] charArr={' ','J','a','v','a'}; newStr.append(charArr); System.out.println("StringBuffer Object="+newStr); } }
Output
append(char[] str, int offset, int len)
You can append only a few characters from a given character array. In this overloaded version of Java append needs 3 arguments.
The first argument is the character array, second argument is the index from where you want to append the characters and last argument is the count of characters you wish to append. The chosen subarray of characters is converted to corresponding string representation. It is added at the end of the StringBuffer Object value.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are Learning"); char[] charArr={' ','J','a','v','a',' ','P','r','o','g','r','a','m','m','i','n','g'}; newStr.append(charArr,5,6); System.out.println("StringBuffer Object="+newStr); } }
Output
append(CharSequence s)
This way of using append method allows you to add a Character Sequence at the end of the character sequence defined for the StringBuffer object. Only one argument of charSequence is needed here.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are Learning "); CharSequence chSeq="Programming in Java with CSVeda.com"; newStr.append(chSeq); System.out.println("StringBuffer Object="+newStr); } }
Output
append(CharSequence s, int start, int end)
This overloaded version of append method allows to append a subsequence of a given CharSequence to the StringBuffer object sequence. It has 3 arguments.
The first is the source character sequence from which the subsequence is to be appended. The second argument is the index of the character from where to pick the sub sequence. The third argument is the last index of the subsequence.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are Learning "); CharSequence chSeq="Programming in Java with CSVeda.com"; newStr.append(chSeq,4,20); System.out.println("StringBuffer Object="+newStr); } }
Output
append(double d)
This version of append method attaches the string representation of the double argument to the end of character sequence stored in the StringBuffer object.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are Learning about append method with double as argument "); double dblVal=2093.567; newStr.append(dblVal); System.out.println(newStr); } }
Output
append(float f)
This version of append is used to add string representation of the float argument passed to the method. The conversion to string representation of a float value is done implicitly.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are Learning about append method with float as argument "); float fltVal=900; newStr.append(fltVal); System.out.println(newStr); } }
Output
append(int i)
The append method with integer value as argument adds its string representation at the end of the character sequence of the StringBuffer Object.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are learning about append method with Integer as argument "); int intVal=97300; newStr.append(intVal); System.out.println(newStr); } }
Output
append(long lng)
The append method with long value as argument adds its string representation at the end of the character sequence of StringBuffer Object.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are learning about append method with Long as argument "); long lngVal=9730890; newStr.append(lngVal); System.out.println(newStr); } }
Output
append(Object obj)
This version of append method accepts an object as the only argument needed. The object is converted to its string representation and is added at the end of the character sequence of StringBuffer Object.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are learning about append method with Object as argument "); Object objObj=" Object value"; newStr.append(objObj); System.out.println(newStr); } }
Output
append(String str)
In this version of append method a String object is passed as an argument and it is appended to the StringBuffer character sequence.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are learning about append method with String as argument "); String strObj=" String value"; newStr.append(strObj); System.out.println(newStr); } }
Output
append(StringBuffer sb)
This last way of append method is used to append a StringBuffer object after another StringBuffer Object. Single argument of type StringBuffer is required in this overloaded method.
Code Example
public class StrBufDemo { public static void main (String args[]) { StringBuffer newStr=new StringBuffer("You are learning about append method with StringBuffre object as argument "); StringBuffer strObj= new StringBuffer("StringBuffer object"); newStr.append(strObj); System.out.println(newStr); } }