Java StringBuffer append() Method

Java StringBuffer append() Method | In this blog, we will discuss the append() method given in the StringBuffer class. The append() method appends two or more strings. The append() method of the StringBuffer class is very similar to the StringBuilder class append() method.

The append() method of the StringBuffer class has the following different variations:-

  1. public synchronized StringBuffer append(String str)
  2. public synchronized StringBuffer append(Object obj)
  3. public synchronized StringBuffer append(StringBuffer sb)
  4. public synchronized StringBuffer append(CharSequence s)
  5. public synchronized StringBuffer append(CharSequence s, int start, int end)
  6. public synchronized StringBuffer append(char[ ] str)
  7. public synchronized StringBuffer append(char[ ] str, int offset, int len)
  8. public synchronized StringBuffer append(boolean b)
  9. public synchronized StringBuffer append(char c)
  10. public synchronized StringBuffer append(int i)
  11. public synchronized StringBuffer append(long lng)
  12. public synchronized StringBuffer append(float f)
  13. public synchronized StringBuffer append(double d)

Java StringBuffer append() Example

Let us see some examples of the StringBuffer class append() method. We will initialize an empty StringBuffer object and add some values to it using the StringBuffer class.

public class Main {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer();
      sb.append("Hi ");
      sb.append("John, ");
      sb.append("How ");
      sb.append("are ");
      System.out.println("Result = " + sb);


Result = Hi John, How are you?

Let us see an example of the append() method by appending the char array.

public class Main {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Welcome to ");
      char[] ch = { 'K', 'n', 'o', 'w', 'P', 'r', 'o', 'g', 'r', 'a', 'm' };
      System.out.println("After appending = " + sb);


After appending = Welcome to KnowProgram

StringBuffer append() In Java Example

By using the public StringBuffer append(boolean bool) method of the StringBuffer class, we can append boolean values to the StringBuffer objects.

public class Main {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Are you ok? ");
      System.out.println("Result after appending = " + sb);



Result after appending = Are you ok? true

Java StringBuffer Append New Line

To append a new line we can use the append(char ch) method. The syntax for this method is as follows:- public StringBuffer append(char ch)
Parameters: ch: the character that needs to be appended.
Return type: StringBuffer.
Returns: the StringBuffer after appending the character.

public class Main {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Hello");
      System.out.println("The result after appending = " + sb);


The result after appending = Hello

StringBuffer Java Append

From a given array of characters, we can also append only a few portions of the array to the StringBuffer object. The following method can be used for this purpose:- public StringBuffer append(char[ ] array, int index, int len)
array: The array which will be appended
Index: specifies the index of the first character to be appended.
len: says the number of characters to be appended.

public class Main {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Work ");
      System.out.println("StringBuffer before appending = " + sb);
      char[] array = 
         { 'o', 'n', 'l', 'i', 'n', 'e', 'o', 'r', 'o', 'f', 'f', 'l', 'i', 'n', 'e' };
      sb.append(array, 0, 6);
      System.out.println("StringBuffer after appending = " + sb);


StringBuffer before appending = Work
StringBuffer after appending = Work online

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *