What is the append method in Java?


The append(char c) method of the java.lang.StringBuffer appends the string representation of the char argument to this sequence. The argument is appended to the contents of this sequence. The length of this sequence increases by 1.

Example

 Live Demo

import java.lang.*;
public class StringBufferDemo {
   public static void main(String[] args) {
      StringBuffer buff = new StringBuffer("tuts ");
      System.out.println("buffer = " + buff);
     
      // appends the char argument as string to the string buffer.
      buff.append('A');
     
      // print the string buffer after appending
      System.out.println("After append = " + buff);
      buff = new StringBuffer("abcd ");
      System.out.println("buffer = " + buff);
     
      // appends the char argument as string to the string buffer.
      buff.append('!');
     
      // print the string buffer after appending
      System.out.println("After append = " + buff);
   }
}

Output

buffer = tuts
After append = tuts A
buffer = abcd
After append = abcd !

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements