Java StringBuffer class

  • June 16, 2022
  • Java
java thread class

StringBuffer is a String peer class that provides a lot of the same functionality as strings. StringBuffer represents growable and writable character sequences, whereas a string represents fixed-length, immutable character sequences. Characters and substrings can be inserted in the middle or appended to the end of a StringBuffer.

Constructors of StringBuffer class :

1. StringBuffer(): It saves 16 characters by not reallocating space.

StringBuffer s = new StringBuffer();

2. StringBuffer( int size): It accepts an integer argument that specifies the buffer’s size.

StringBuffer s = new StringBuffer(20);

3. StringBuffer(String str): It takes a string argument that sets the initial contents of the StringBuffer object and frees up space for 16 more characters without reallocating space.

StringBuffer s = new StringBuffer("studentscoding");
Important methods of StringBuffer class:
Modifier and TypeMethodDescription
public synchronized StringBufferappend(String s)It is used to append the specified string with this string. Like append(char), append(boolean), append(int), append(float), append(double), etc., the append() function has many overloads.
public synchronized StringBufferinsert(int offset, String s)It is used to insert the specified string with this string at the specified position. There are several other overloaded versions of the insert() method, including insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double), etc.
public synchronized StringBufferreplace(int startIndex, int endIndex, String str)It replaces the string from specified startIndex and endIndex.
public synchronized StringBufferdelete(int startIndex, int endIndex)It deletes the string from specified startIndex and endIndex.
public synchronized StringBufferreverse()it reverses the string.
public intcapacity()It returns the current capacity.
public voidensureCapacity(int minimumCapacity)It ensures the capacity is at least equal to the given minimum.
public charcharAt(int index)It returns the character at the specified position.
public intlength()It returns the length of the string i.e. total number of characters.
public Stringsubstring(int beginIndex)It returns the substring from the specified beginIndex.
public Stringsubstring(int beginIndex, int endIndex)It returns the substring from the specified beginIndex and endIndex.
Example: length(),capacity(),append(), reverse(), replace(), ensurecapacity()

import java.io.*;

// Main class
class Demo {

	// main driver method
	public static void main(String[] args)
	{

		// Creating adn storing string by creating object of
		// StringBuffer
		StringBuffer s = new StringBuffer("Coderzpy");

		// Getting the length of the string
		int p = s.length();

		// Getting the capacity of the string
		int q = s.capacity();
			System.out.println("Length of string "+s+"="
						+ p);
		System.out.println(
			"Capacity of string "+s+"=" + q);
		s.append(".com");

		
		System.out.println("Length of string "+s+"="
						+ s.length());
		System.out.println(
			"Capacity of string "+s+"=" + s.capacity());
			System.out.println("reverse = "+s.reverse());
			
			
        s.deleteCharAt(7);
        // Returns string after deletion
        System.out.println("deleted "+s);
        
         s.replace(5, 8, "are");
 
       
        System.out.println("replaced with are"+s);

        s.ensureCapacity(40);
         	System.out.println(
			"Capacity of string "+s+"=" + s.capacity());
        
	}
}
Output:
Length of string Coderzpy=8
Capacity of string Coderzpy=24
Length of string Coderzpy.com=12
Capacity of string Coderzpy.com=24
reverse = moc.ypzredoC
deleted moc.ypzedoC
replaced with aremoc.yaredoC
Capacity of string moc.yaredoC=50

Note: also read about the Some examples of string class methods

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

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