Java StringBuffer class.

Java 8Object Oriented ProgrammingProgramming

The java.lang.StringBuffer class is a thread-safe, mutable sequence of characters. Following are the important points about StringBuffer −

  • A string buffer is like a String but can be modified.
  • It contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
  • They are safe for use by multiple threads.
  • Every string buffer has a capacity.

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 boolean argument as string to the string buffer
      buff.append(true);

      // print the string buffer after appending
      System.out.println("After append = " + buff);
      buff = new StringBuffer("abcd ");
      System.out.println("buffer = " + buff);

      // appends the boolean argument as string to the string buffer
      buff.append(false);

      // print the string buffer after appending
      System.out.println("After append = " + buff);
   }
}

Output

buffer = tuts
After append = tuts true
buffer = abcd
After append = abcd false
raja
Published on 12-Feb-2018 09:04:10
Advertisements