StringBuffer class in Java

JShellJShell CommandsREPL

In the previous blog, we learned about the methods of String. If you want to know more about the methods of String, then visit String Methods in Java - Part 2. In this blog, we will learn about the StringBuffer class, its methods, etc. In general, StringBuffer works the same way String works but only the difference it is mutable (the contents of StringBuffer can be changed). In StringBuffer characters and substrings can be inserted in the middle or appended to the end. Now we will go through its Constructors and Methods.

Constructors

StringBuffer();

Creates an empty String Buffer containing the initial capacity of 16.

StringBuffer(String string);

Creates String Buffer with the specified string

StringBuffer(int capacity);

Creates an empty String Buffer with the specified length mentioned as capacity


Methods of StringBuffer Class

  1. append()

    append() is used for concatenating the specified string with the given string. It can even concatenate int, float, char, etc.

    Syntax

    public StringBuffer append(String string);
    public StringBuffer append(int value);
    public StringBuffer append(float floatValue);
    public StringBuffer append(double doubleValue);
    public StringBuffer append(boolean booleanValue); etc.

    e.g

    public class AppendEx {
      public static void main(String args[]) {
          StringBuffer strBuffer=new StringBuffer("Welcome to ");
          strBuffer.append("Java Shortkicks");
          System.out.println(strBuffer);
    
          Integer value = new Integer(99);
          StringBuffer strBuffer1 = new StringBuffer("The value of the given number is: ");
          strBuffer1.append(value);
          System.out.println(strBuffer1);
    
          StringBuffer strBuffer2 = new StringBuffer("The value of the given number is: ");
          Float floatValue = new Float(23.6f);
          strBuffer2.append(floatValue);
          System.out.println(strBuffer2);
    
          StringBuffer strBuffer3 = new StringBuffer("The value of the given number is: ");
          Character character = new Character('S');
          strBuffer3.append(character);
          System.out.println(strBuffer3);
    
          StringBuffer strBuffer4 = new StringBuffer("The value of the given number is: ");
          Double doubleValue = new Double(43.786d);
          strBuffer4.append(doubleValue);
          System.out.println(strBuffer4);
    
          StringBuffer strBuffer5 = new StringBuffer("The value of the given number is: ");
          Boolean booleanValue = new Boolean(true);
          strBuffer5.append(booleanValue);
          System.out.println(strBuffer5);
      }
    }

    Output

    Welcome to Java Shortkicks
    The value of the given number is: 99
    The value of the given number is: 23.6
    The value of the given number is: S
    The value of the given number is: 43.786
    The value of the given number is: true
  2. insert()

    insert() is used when we want to insert a particular string at the specified location. We can also insert different types of values like int, float, char, double, etc.

    Syntax

    public StringBuffer insert(int index, String string);
    public StringBuffer insert(int index, int value);
    public StringBuffer insert(int index, float value);
    public StringBuffer insert(int index, char character);
    public StringBuffer insert(int index, double value);
    public StringBuffer insert(int index, boolean value); etc.

    e.g

    public class InsertEx {
      public static void main(String args[]) {
          StringBuffer stringBuffer=new StringBuffer("Welcome Java Shortkicks");
          stringBuffer.insert(8,"to ");
          System.out.println(stringBuffer);
    
          stringBuffer.insert(7,9);
          System.out.println(stringBuffer);
    
          stringBuffer.insert(9,45.2f);
          System.out.println(stringBuffer);
    
          stringBuffer.insert(13,'S');
          System.out.println(stringBuffer);
    
          stringBuffer.insert(15,546.89d);
          System.out.println(stringBuffer);
    
          stringBuffer.insert(2,true);
          System.out.println(stringBuffer);
    
          char charArray[] = {'a','t','r','o','w','e','l'};
          stringBuffer.insert(2,charArray);
          System.out.println(stringBuffer);
      }
    }

    Output

    Welcome to Java Shortkicks
    Welcome9 to Java Shortkicks
    Welcome9 45.2to Java Shortkicks
    Welcome9 45.2Sto Java Shortkicks
    Welcome9 45.2St546.89o Java Shortkicks
    Wetruelcome9 45.2St546.89o Java Shortkicks
    Weatroweltruelcome9 45.2St546.89o Java Shortkicks
  3. replace()

    replace() replaces the specified string with the particular start index and end index.

    Syntax

    public StringBuffer(int startIndex, int endIndex, String string);

    e.g

    public class ReplaceEx {
      public static void main(String args[]) {
          StringBuffer strBuffer = new StringBuffer("Java Shortkicks");
          StringBuffer strBuffer1 = new StringBuffer("Welcome To Java Shortkicks");
          strBuffer.replace(4,7, "for");
          strBuffer1.replace(5,9, "from");
          System.out.println(strBuffer);
          System.out.println(strBuffer1);
      }
    }

    Output

    Javaforortkicks
    Welcofromo Java Shortkicks
  4. delete()

    delete() is used for deleting the string from the given start index and end index.

    Syntax

    public StringBuffer delete(int startIndex, int endIndex);

    e.g

    public class DeleteEx {
      public static void main(String args[]) {
          StringBuffer strBuffer = new StringBuffer("Shortkicks");
          strBuffer.delete(1,5);
          System.out.println(strBuffer);
      }
    }

    Output

    Skicks
  5. reverse()

    reverse() is used for reversing the current String.

    Syntax

    public StringBuffer reverse();

    e.g

    public class ReverseEx {
      public static void main(String args[]) {
          StringBuffer strBuffer = new StringBuffer("Java Shortkicks");
          strBuffer.reverse();
          System.out.println(strBuffer);
      }
    }

    Output

    skciktrohS avaJ

Difference between String and StringBuffer Class

FeaturesStringStringBuffer
MutableString is immutableStringBuffer is mutable
StorageString uses constant poolStringBuffer uses heap memory
PerformanceString is slow during concatenationStringBuffer is fast during concatenation
MemoryString requires more memory compared to StringBufferStringBuffer requires less memory as compared to String
Overrides MethodString overrides equal() method of Object classStringBuffer doesn't override equal() of the Object class

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.