StringBuffer in Java – Explore its Constructors & Methods with Examples

Do you want to manipulate strings but, it doesn’t allow? StringBuffer in Java is specially designed for that. StringBuffer allows us to append, replace, reverse, concatenate or any kind of manipulation on strings.

Java StringBuffer offers constructors and methods to deal with any kind of manipulation.

Before we start our article, you should be well familiar with Strings in Java.

So, what are you waiting for? Let’s start with the introduction.

What is StringBuffer in Java?

StringBuffer in java is mutable that means it can be changed. StringBuffer represents growable and writable characters, whereas String represents fixed-length, immutable characters. A string buffer is comparatively fast as compared to a string class. We can also append the character in the middle of the string with the help of the append method.

Constructors in StringBuffer in Java

  • StringBuffer( ): It reserves space for sixteen characters while not reallocation.
StringBuffer s=new StringBuffer();
  • StringBuffer( int size): It accepts a whole number argument that expressly sets the scale of the buffer.
StringBuffer s=new StringBuffer(20);
  • StringBuffer(String str): It reserves area for sixteen a lot of characters while not reallocation and accepts a String argument that sets the initial contents of the StringBuffer object.
StringBuffer s=new StringBuffer("HELLO");

Do you know String vs StringBuffer vs StringBuilder in Java?

StringBuffer Methods

StringBuffer methods in java

Following are the Java StringBuffer Methods-

1. length( ) and capacity( )

The length of the string can be identified by the length( ) method, whereas the capability of the string can be identified by the capacity( ) method.

Example

package com.DataFlair.StringBuffer;

public class StringBufferDemo {
 public static void main (String[] args)
     {
         StringBuffer stringName=new StringBuffer("HELLO DATAFLAIR");
         int lengthVar=stringName.length();
         int CapacityVar=stringName.capacity();
         System.out.println("Length of string DATAFLAIR is = "+lengthVar);
         System.out.println("Capacity of string DATAFLAIR is = "+CapacityVar);
     }
  }

Output-string-buffer-demo

Must Read – StringTokenizer in Java

2. append( )

It is used to add text at the end of the existing text.

Example-

package com.DataFlair.StringBuffer;

public class StringBufferAppend 
{
 public static void main (String[] args)
     {
         StringBuffer stringName=new StringBuffer("Data");
         stringName.append("Flair");
         System.out.println(stringName);
         stringName.append("Web Services");
         System.out.println(stringName);    
     }
  }

Output-

string-Buffer-append

3. insert( )

It is used to insert a character or a string at a specified position. Here, index specifies the index at which point the string will be inserted into the invoking StringBuffer object.

Recommended Reading – Java Character Class with Syntax & Example

Example 

package com.DataFlair.StringBuffer;

public class StringBufferInsert {
     public static void main (String[] args)
     {
         StringBuffer stringName=new StringBuffer(" Data");
         stringName.insert(5, " Sudama Nagar ");
         System.out.println(stringName);
         stringName.insert(5, " Indore ");
         System.out.println(stringName);
         stringName.insert(5, " Services ");
         System.out.println(stringName);
         stringName.insert(5, " Web ");
         System.out.println(stringName);
         stringName.insert(5, "Flair");
         System.out.println(stringName);
         
  
     }
  }

Output –String-Buffer-Insert

4. reverse( )

It will reverse the given characters using reverse( ) method. This method returns the reverse object.

Example-

package com.DataFlair.StringBuffer;

public class StringBufferReverse {

   public static void main (String[] args)
     {
       StringBuffer stringName=new StringBuffer("DataFlair Web Services");
       stringName.reverse();
       System.out.println(stringName);        }
  }

Output-

String-Buffer-Reverse

5. delete( ) and deleteCharAt( )

The delete( ) method deletes a sequence of characters from the calling object. Here, begin Index specifies the index of the primary character to get rid of, and finish Index specifies an index one past the last character to get rid of.  It will delete characters at intervals an of StringBuffer in Java by with the help of delete( ) and deleteCharAt( ). Thus, the substring deleted runs from begin Index to endIndex–1. The ensuing StringBuffer object comes back. The deleteCharAt( ) technique deletes the character at the index fixed by loc. It returns the ensuing StringBuffer object.

It’s time to test your knowledge with Latest Java Quiz

These strategies are:

  • StringBuffer delete(int startIndex, int endIndex)
  • StringBuffer deleteCharAt(int loc)

Example-

package com.DataFlair.StringBuffer;

public class StringBufferDelete {
 public static void main (String[] args)
     {
         StringBuffer stringName=new StringBuffer("DataFlair");
         stringName.delete(0,4);
         System.out.println(stringName);
         stringName.deleteCharAt(2);
         System.out.println(stringName);    
}
  }

Output-String-Buffer-Delete

6. replace( )

It will replace one set of characters with another set within a StringBuffer object using replace( ) method. The substring replaces fix by the indexes begin to index and endIndex. Thus, the substring at begin Index through endIndex–1 replace.

The signature is – 

StringBuffer replace(int startIndex, int endIndex, String str)

Example 

package com.DataFlair.StringBuffer;

public class StringBufferReplace {
     public static void main (String[] args)
     {
         StringBuffer stringName=new StringBuffer("Data");
         stringName.replace(4,8,"Flair");
         System.out.println(stringName);    
         }
  }

Output-String-Buffer-Replace

7. ensureCapacity( )

ensurecapacity() will use to increase the capability of a StringBuffer object. The new capability is set to either double or capacity+2, whichever is larger. Here, capability specifies the dimensions of the buffer.

void ensureCapacity(int capacity).

Java Interview Questions helps you to crack your next Interview!!

Java Quiz

Summary

To sum up, we can say StringBuffer in Java only used when multiple threads are modifying its contents. It is comparatively faster than String.  With the help of constructors and methods, we learned every detail about Java StringBuffer. Now, start practicing!

Hope, you like this article. Please share your suggestion and feedback in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.