Call append() method to construct a StringBuffer object in Java


The StringBuffer append() method appends the String representation of the particular argument to the sequence. It is a method of the java.lang.StringBuffer class. This method returns a reference to the object.

The method changes the value of the object called in the method. Each primitive data type will have a separate method −

public StringBuffer append(double d)
public StringBuffer append(float f)
public StringBuffer append(int i)
public StringBuffer append(String str)
public StringBuffer append(long l)
public StringBuffer append(char[] str)
public StringBuffer append(char[] str, int offset, int len)
public StringBuffer append(Object obj)
public StringBuffer append(boolean b)
public StringBuffer append(char c)
public StringBuffer append(StringBuffer sb)

Example

A program to illustrate the use of append() method to construct a StringBuffer Object is given as follows −

 Live Demo

import java.lang.*;
public class Example {
   public static void main(String[] args) {
      StringBuffer s1 = new StringBuffer("Hello ");
      System.out.println("Statement : " + s1);
      s1.append("World");
      System.out.println("Output: " + s1);
   }
}

Output

Statement : Hello
Output: Hello World

Updated on: 26-Jun-2020

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements