• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Test Cases
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Java StringBuffer Class with Example

Java StringBuffer Class with Example

Last Updated on: May 9, 2022 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • What is StringBuffer in Java?
  • Syntax of StringBuffer class
  • StringBuffer Class Declaration
  • Constructors in the StringBuffer Class
  • Methods of StringBuffer Class in Java:

Hey there! We’ve talked about Strings in Java a bunch of times in our articles, and we’ve used them in several java programs too. A String is just a collection of characters. In Java, Strings are important because they can be manipulated in many ways. As a Java developer, it is essential to learn how to manipulate Strings so that you can create code that is effective and efficient.

Welcome! This article will introduce you to the String manipulation in Java with the StringBuffer class, which can help you manipulate Strings in many ways. We’ll also discuss various methods and constructors of the Java StringBuffer class.

Post On:StringBuffer Class In Java
Post Type:Java Tutorials
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:SoftwareTestingo Telegram Group

What is StringBuffer in Java?

In Java, strings are immutable. This means that once you create a string object, its value cannot be changed. If we want to alter or assign the new value to the string is not possible. It creates a string object and then assigns the value. Here, the String Buffer class comes into play. With the help of String Buffer, we can modify objects within a string buffer which allows us to change our initial string.

Java Tutorial
  • Java String subsequence
  • Java String compareTo()
  • Java String substring
  • Java Split String
  • Java String concatenation
  • StringBuilder Class In Java
  • Java StringBuffer Class

Syntax of StringBuffer class

So, the Object class is considered to be a parent of all classes. The StringBuffer class inherits from the Object class as well as two interfaces – CharSequence and Serializable. This makes it a powerful and versatile tool for developers.

String Buffer Class Syntax
String Buffer Class Syntax
public final class StringBuffer
    extends Object
    implements Serializable, CharSequence

StringBuffer Class Declaration

StringBuffer is declared using the new keyword, just like all other classes in Java. However, StringBuffer is not a primitive class, so you cannot directly assign a value to it. For example, this statement is invalid: StringBuffer str = value.

StringBuffer<object_name> = new StringBuffer();

With the object_name reference, we can access various in-built methods of the StringBuffer class.

In Java two define a class we used two things:

  • Constructor
  • Method

Constructors in the StringBuffer Class

There are different types of constructors in StringBuffer that can help you convert an ordinary String or characters into a StringBuffer, as well as configure properties such as size. Let us try to discuss the constructors of StringBuffer in Java:

StringBuffer():

This constructor is used for creating a StringBuffer object with an initial capacity of 16 bytes. The Capacity refers to the maximum number of elements that the StringBuffer can store. This constructor doesn’t require any parameters.

StringBuffer ob = new StringBuffer();

StringBuffer(int capacity):

This constructor is used for initializing the initial size of the StringBuffer. The capacity parameter indicates how many characters the StringBuffer can store before it needs to resize itself. This constructor takes one parameter i.e. the capacity of the StringBuffer.

//Initialising the StringBuffer capacity to the 10 bytes
StringBuffer ob = new StringBuffer(10); 

StringBuffer(String):

This constructor can be used to convert a String object into a StringBuffer object. The default capacity of the new StringBuffer will be 16 bytes.

If the capacity of StringBuffer gets full after adding an extra String, the new capacity of StringBuffer will be: (previousCapacity+1)*2

If the initial capacity is 10, then after full the updated capacity becomes (10+1)*2=22

Methods of StringBuffer Class in Java:

If you’re looking to perform different operations on a StringBuffer in Java, there are various built-in methods that can help you do so. Let’s take a look at each method one by one.

StringBuffer: append() method

This method can be used to append a new sequence of characters to an existing character sequence in the StringBuffer class.

package com.SoftwareTestingO.Strings;
public class StringBufferAppend 
{
	public static void main(String[] args) 
	{
		StringBuffer str = new StringBuffer("Software");
		// appends a string in the previously defined string.
		str.append("Testingo"); 
		System.out.println(str);
	}
}

Adding a value to an empty StringBuffer will not cause any errors.

StringBuffer: length() method

The length of a StringBuffer sequence can be found by using the method .length(). Length is defined as the number of elements in the StringBuffer sequence.

package com.SoftwareTestingO.Strings;
public class StringBufferLength 
{
	public static void main(String[] args) 
	{
		StringBuffer str = new StringBuffer("SoftwareTestingo");
		int len=str.length();
		System.out.println("The Length Of String Buffer Object: "+len);
	}
}

StringBuffer: capacity() method

This method is used for finding the initial capacity of the StringBuffer object. The default capacity of the StringBuffer class is 16 bytes, but it can be increased as needed.

package com.SoftwareTestingO.Strings;
public class StringBufferCapacity 
{
	public static void main(String[] args) 
	{
		StringBuffer str = new StringBuffer("SoftwareTestingo");
		int len=str.capacity();
		System.out.println("The Capacity Of String Buffer Object: "+len);
	}
}

StringBuffer: insert() method

Use the insert method to add a string at a particular indexed position. This method takes two arguments: the index where you want to insert the new string, and the string itself.

package com.SoftwareTestingO.Strings;
public class StringBufferInsert 
{
	public static void main(String[] args) 
	{
		StringBuffer str = new StringBuffer("Welcome");
		System.out.println(str);

		str.insert(7, " to ");
		System.out.println(str);

		str.insert(10, " SoftwareTestingo ");
		System.out.println(str);
	}
}

StringBuffer: reverse() method

The reverse() method of the StringBuffer class reverses all the characters in the object and returns a reversed String object.

package com.SoftwareTestingO.Strings;
public class StringBufferReverse 
{
	public static void main(String[] args) 
	{
		StringBuffer str = new StringBuffer("SoftwareTestingo");
		System.out.println("Original String: " + str);
		str.reverse();
		System.out.println("Reversed String: " + str);
	}
}

StringBuffer: delete() method

The delete() method of the StringBuffer class deletes a sequence of characters from the StringBuffer object, taking two arguments: the starting index of the string to delete, and the last index up to which to delete.

package com.SoftwareTestingO.Strings;
public class StringBufferDelete 
{
	public static void main(String[] args) 
	{
		StringBuffer str = new StringBuffer("Welcome To SoftwareTestingo");
		System.out.println("Original String: " + str);
		str.delete(8,10);
		System.out.println("After Delete String: " + str);
	}
}

StringBuffer deleteCharAt() Method

This method deletes the character at a given index from a StringBuffer sequence.

package com.SoftwareTestingO.Strings;
public class StringBufferDelete 
{
	public static void main(String[] args) 
	{
		StringBuffer str = new StringBuffer("Welcome To SoftwareTestingo");
		System.out.println("Original String: " + str);
		str.deleteCharAt(8);
		System.out.println("After Delete Character at 8 position: " + str);
	}
}

Conclusion

The Java StringBuffer class is useful for performing various operations on strings, such as concatenation, insertion, deletion, and reversal. This allows for greater control over how strings are manipulated to achieve the desired results.

This article provides an overview of the StringBuffer class and how it can be used. If still if you have any questions then let us know in the comment section and we are happy to help you.

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2022 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers