Java StringBuffer append() Method Examples

This page shows the usage of all Java StringBuffer append() methods with an example.

Java StringBuffer append() Methods

StringBuffer append() method has several overloaded versions. Here are all its forms:
  1. StringBuffer append(boolean b) - Appends the string representation of the boolean argument to the sequence.
  2. StringBuffer append(char c) - Appends the string representation of the char argument to this sequence.
  3. StringBuffer append(char[] str) - Appends the string representation of the char array argument to this sequence.
  4. StringBuffer append(char[] str, int offset, int len) - Appends the string representation of a subarray of the char array argument to this sequence.
  5. StringBuffer append(CharSequence s) - Appends the specified CharSequence to this sequence.
  6. StringBuffer append(CharSequence s, int start, int end) - Appends a subsequence of the specified CharSequence to this sequence.
  7. StringBuffer append(double d) - Appends the string representation of the double argument to this sequence.
  8. StringBuffer append(float f) - Appends the string representation of the float argument to this sequence.
  9. StringBuffer append(int i) - Appends the string representation of the int argument to this sequence.
  10. StringBuffer append(long lng) - Appends the string representation of the long argument to this sequence.
  11. StringBuffer append(Object obj) - Appends the string representation of the Object argument.
  12. StringBuffer append(String str) - Appends the specified string to this character sequence.
  13. StringBuffer append(StringBuffer sb) - Appends the specified StringBuffer to this sequence.
  14. StringBuffer appendCodePoint(int codePoint) - Appends the string representation of the codePoint argument to this sequence.

Java StringBuffer append() Methods with Examples

Let's demonstrate each append() method with examples.
Example: This program demonstrates the usage of above 14 append methods.
public class AppendExample {
 public static void main(String[] args) {
  // 14 append overloaded methods

  // Append String
  StringBuffer buffer;
  buffer = new StringBuffer().append("guides");
  System.out.println("Append String : " + buffer);

  // Append char
  buffer = new StringBuffer().append('c');
  System.out.println("Append char : " + buffer);

  // Append Object
  buffer = new StringBuffer().append(new Object().getClass());
  System.out.println("Append Object : " + buffer);

  // Append chars
  char[] chars = { 'j', 'a', 'v', 'a' };
  buffer = new StringBuffer().append(chars);
  System.out.println("Append chars : " + buffer);

  // Append charSequence
  CharSequence charSequence = new String("charSequence");
  buffer = new StringBuffer().append(charSequence);
  System.out.println("Append charSequence : " + buffer);

  // Append Double
  buffer = new StringBuffer().append(10.0d);
  System.out.println("Append Double : " + buffer);

  // Append Float
  buffer = new StringBuffer().append(10.5f);
  System.out.println("Append Float : " + buffer);

  // Append int
  buffer = new StringBuffer().append(100);
  System.out.println("Append int : " + buffer);

  // Append Boolean
  buffer = new StringBuffer().append(true);
  System.out.println("Append Boolean : " + buffer);

  // Append Long
  buffer = new StringBuffer().append(1000);
  System.out.println("Append Long : " + buffer);

  // Append stringbuffer
  buffer = new StringBuffer().append(new StringBuffer("stringbuffer"));
  System.out.println("Append stringbuffer : " + buffer);

  // Appends the string representation of a subarray of the char array
  // argument to this sequence.
  buffer = new StringBuffer().append(chars, 1, 3);
  System.out.println("Appends the string representation of a "
    + " subarray of the char array argument to this sequence.  : " + buffer);

  // Appends a subsequence of the specified CharSequence to this sequence
  buffer = new StringBuffer().append("javaguides", 0, 9);
  System.out.println("Appends a subsequence of the specified " 
  + " CharSequence to this sequence. : " + buffer);

  // Appends the string representation of the codePoint argument to this
  // sequence.
  buffer = new StringBuffer().appendCodePoint(5);
  System.out.println(
    "Appends the string representation of the "
  + " codePoint argument to this sequence.  : " + buffer);
 }
}
Output:
Append String : guides
Append char : c
Append Object : class java.lang.Object
Append chars : java
Append charSequence : charSequence
Append Double : 10.0
Append Float : 10.5
Append int : 100
Append Boolean : true
Append Long : 1000
Append stringbuffer : stringbuffer
Appends the string representation of a  subarray of the char array argument to this sequence.  : ava
Appends a subsequence of the specified  CharSequence to this sequence. : javaguide
Appends the string representation of the  codePoint argument to this sequence.  : 



Comments