What is the append method in Java?

Java 8Object Oriented ProgrammingProgramming

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 !
raja
Published on 09-Feb-2018 11:31:50
Advertisements