StringBuffer class

In this chapter you will learn:

  1. Why do we need StringBuffer
  2. How to create StringBuffer

Why do we need StringBuffer

String represents fixed-length, immutable character sequences.

StringBuffer represents growable and writeable character sequences.

StringBuffer will automatically grow to make room for such additions. Java manipulate StringBuffers behind the scenes when you are using the overloaded + operator.

Create StringBuffer

  • StringBuffer()
    Creates a string buffer with no characters in it and an initial capacity of 16 characters.
  • StringBuffer(CharSequence seq)
    Creates a string buffer that contains the same characters as the specified CharSequence.
  • StringBuffer(int capacity)
    Creates a string buffer with no characters in it and the specified initial capacity.
  • StringBuffer(String str)
    Creates a string buffer initialized to the contents of the specified string.
public class Main {
  public static void main(String[] argv) {
    StringBuffer sb = new StringBuffer(20);
    System.out.println(sb.capacity());//from  ja  v  a2 s. co  m

The output:

Using new StringBuffer(String str)

public class Main {
/*from  j a va2s  .  c  o  m*/
  public static void main(String[] arg) {
    StringBuffer aString = new StringBuffer("ABCDE");

    String phrase = "abced";
    StringBuffer buffer = new StringBuffer(phrase);



The followoing code always insert to the start of a StringBuffer.

public class Main {
  public static void main(String args[]) {
    Object objectRef = "hello";
    String string = "goodbye";
    char charArray[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    boolean booleanValue = true;
    char characterValue = 'K';
    int integerValue = 7;
    long longValue = 10000000;
    float floatValue = 2.5f;
    double doubleValue = 33.3;
/* ja v a 2 s .co m*/
    StringBuffer buffer = new StringBuffer();

    buffer.insert(0, objectRef);
    buffer.insert(0, "  ");
    buffer.insert(0, string);
    buffer.insert(0, "  ");
    buffer.insert(0, charArray);
    buffer.insert(0, "  ");
    buffer.insert(0, charArray, 3, 3);
    buffer.insert(0, "  ");
    buffer.insert(0, booleanValue);
    buffer.insert(0, "  ");
    buffer.insert(0, characterValue);
    buffer.insert(0, "  ");
    buffer.insert(0, integerValue);
    buffer.insert(0, "  ");
    buffer.insert(0, longValue);
    buffer.insert(0, "  ");
    buffer.insert(0, floatValue);
    buffer.insert(0, "  ");
    buffer.insert(0, doubleValue);

    System.out.printf("buffer after inserts:\n%s\n\n", buffer.toString());


The output:

Next chapter...

What you will learn in the next chapter:

  1. How to append to StringBuffer
  2. How to use the chained append() method
  3. How to add data in the middle of a StringBuffer
Home » Java Tutorial » String


    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace


    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String


    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words