StringBuffer In Java | StringBuffer Constructor and Methods

1. Java StringBuffer – Objective

In this Java String tutorial, we are going to study StringBuffer in Java Programming Langauge. Moreover, we will study StringBuffer constructors and, methods of StringBuffer in java detail. Along with this, we will discuss an example of each Java StringBuffer method such as StringBuffer append in Java, StringBuffer length and capacity etc.

So, let us start StringBuffer in Java.

StringBuffer In Java | StringBuffer Constructor and Methods

StringBuffer In Java | StringBuffer Constructor and Methods

2. StringBuffer Constructor and Methods

  • The StringBuffer class in Java could be a peer category of String that gives abundant of the practicality of strings.
  • StringBuffer represents growable and writable character sequences whereas String represents fixed-length, immutable character sequences.
  • Java StringBuffer might have characters and substrings inserted within the middle or appended to the tip.
  • It mechanically grows to form space for such additions and infrequently has additional characters reallocated than are literally required, to permit space for growth.

Do you know String vs StringBuffer vs StringBuilder in Java

3. StringBuffer in Java

Java StringBuffer methods and constructors are discussed below:

a. Constructors in StringBuffer

  • 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”);

b. StringBuffer Method in Java

Some of the foremost used ways for StringBuffer in Java are:

Read Java String, Methods, and Constructor – Syntax and Example

i. length( ) and capacity( )

The length of  StringBuffer in Java will be found by the length( ) technique, whereas the whole allotted capability, will be found by the capacity( ) technique.

Example

import java.io.*;
class DataFlair
{
   public static void main (String[] args)
   {
       StringBuffer s=new StringBuffer("HELLO");
       int p=s.length();
       int q=s.capacity();
       System.out.println("Length of string HELLO="+p);
       System.out.println("Capacity of string HELLO="+q);
   }
}

OUTPUT

Length of string DataFlair=13

Capacity of string DataFlair=29

ii. append( )

StringBuffer Append in Java is accustomed add text at the top of the existing text.

Here are a number of its forms –

StringBuffer append(String str)

StringBuffer append(int num)

Java Comparator Interface – Working of Collections.Sort()

Example

import java.io.*;
class DataFlair
{
   public static void main (String[] args)
   {
       StringBuffer s=new StringBuffer("Data");
       s.append("Flair");
       System.out.println(s);
       s.append(1);
       System.out.println(s);    }
}

Output

DataFlair

DataFlair1

iii. insert( )

It’s accustomed insert text at the desired index position. These are a number of forms.

Here, index specifies the index at that purpose the string is inserted into the invoking StringBuffer object

StringBuffer insert(int index, String str)

                    StringBuffer insert(int index, char ch)

Here, index specifies the index at which point the string will be inserted into the invoking StringBuffer object.

Read what is Jar File in Java and How to Create it

Example 

import java.io.*;
class DataFlair
{
   public static void main (String[] args)
   {
       StringBuffer s=new StringBuffer("DataData");
       s.insert(5, "for");
       System.out.println(s);
       s.insert(0, 5);
       System.out.println(s);
       s.insert(3, true);
       System.out.println(s);
       s.insert(5, 41.35d);
       System.out.println(s);
       s.insert(8, 41.35f);
       System.out.println(s);
       char data_arr[] = { 'p', 'a', 'w', 'a', 'n' };
       s.insert(2, data_arr);
       System.out.println(s);
   }
}

Output 

DataforData

5DataforData

5Getrueatafordata

5Getr41.35ueataforData

5Getr41.41.3535ueTaforData

5Gpawanetr41.41.3535ueatafordata

iv. reverse( )

It will reverse the characters at intervals a StringBuffer object exploitation reverse( ). This technique returns the reverse object on that it absolutely refer to as.

Example 

import java.io.*;
class DataFlair
{
   public static void main (String[] args)
   {
       StringBuffer s=new StringBuffer("DataFlair");
       s.reverse();
       System.out.println(s);        }
}

Output

railFataD

Java Method – Declaration and Calling a Java Method

v. delete( ) and deleteCharAt( )

The delete( ) technique deletes a sequence of characters from the invoking 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 a StringBuffer in Java by exploitation the strategies 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.
These strategies are shown here:

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

Example

import java.io.*;
class DataFLair
{
   public static void main (String[] args)
   {
       StringBuffer s=new StringBuffer("DataFlair");
       s.delete(0,5);
       System.out.println(s);
       s.deleteCharAt(7);
       System.out.println(s);    }
}

Output

Flair

Flair

vi. replace( )

It will replace one set of characters with another set within a StringBuffer object by a line of work replace( ). The substring replaces fix by the indexes begin index and endIndex. Thus, the substring at begin Index through endIndex–1 replace. The replacement sstring passin str. hence, the ensuing StringBuffer object comes back.

Its signature is shown here:

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

Method Overloading in Java with Examples

Example 

import java.io.*;
class DataFlair
{
   public static void main (String[] args)
   {
       StringBuffer s=new StringBuffer("Flair");
       s.replace(5,8,"are");
       System.out.println(s);    }
}

Output

DataFarer

vii. ensureCapacity( )

Hence, it’s accustomed to increase the capability of a StringBuffer object. The new capability is set to either the worth we have a tendency to specify or double this capability and 2 (i.e. capacity+2), whichever is larger. Here, capability specifies the dimensions of the buffer.

void ensureCapacity(int capacity).

So, this was all about StringBuffer in Java. Hope you like our explanation.

4. Conclusion

Hence, in this Java tutorial, we have learned a very important class in Java and that is StringBuffer Class. In conclusion, we saw StringBuffer Constructors and its types. At last, we discussed StringBuffer in Java examples with code and output. Finally, we learned methods of StringBuffer in Java. Furthermore, if you have any query, feel free to ask in the comment section. 

See also – Java File Class – java.io.File Class in Java

For reference

Leave a Reply

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