Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
StringBuffer class
3.
Constructors of StringBuffer class
4.
Methods of StringBuffer class
5.
Examples
5.1.
Code 1 
5.2.
Code 2
5.3.
Code 3
5.4.
Code 4
5.5.
Code 5
5.6.
Code 6
5.7.
Code 7
6.
Frequently Asked Questions
7.
Key Takeaways
Last Updated: Mar 27, 2024

StringBuffer Class

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The String is one of the data types in Java. Strings in Java are Objects that are internally supported by a char array. Because arrays are immutable (cannot grow), Strings are also immutable. StringBuffer is a superior class of String that gives more functionality than String.

Also read about -  Iteration Statements in Java, and Duck Number in Java.

StringBuffer class

StringBuffer class is used to make mutable string objects, Which means they can be modified after creation. This represents the growable and writable character sequence. This class gives us the flexibility to alter our String; it is also thread-safe, i.e., it cannot provide the flexibility of accessing multiple threads simultaneously.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Constructors of StringBuffer class

  • StringBuffer(): This constructor helps us to create an empty StringBuffer of 16 characters having its initial capacity.
     
    StringBuffer s = new StringBuffer();

 

  • StringBuffer(String str): This constructor will create a StringBuffer with the specified String.
     
    StringBuffer s = new StringBuffer(“Coding Ninjas”);

 

  • StringBuffer(int cap): This constructor will accept an integer argument that sets the size of the buffer.
     
    StringBuffer s = new StringBuffer(“Coder-Section”);

Also see, Swap Function in Java

Methods of StringBuffer class

  • append(String s): This method concatenates the string passed as an argument.
     
  • insert(int index, String str): This method will insert one String into another. It will insert the String the particular index position defined in the function.
     
  • reverse(): This function will reverse the characters available in the StringBuffer object.
     
  • replace(int start, int last, String s): This function will replace the character from the start index to the last index passed in the method parameter. 
     
  • capacity(): This function will return the capacity of the StringBuffer object.
     
  • ensureCapacity(): This function is used to ensure the minimum capacity of the StringBuffer object.
     
  • delete(int start, int end): This method will delete the String from the specified index passed in the argument list. 

Must Read: Java System Out Println and Hashcode Method in Java.

Examples

Code 1 

public class Ex21 {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer("Coding-Ninjas");
		str.append(123);
		System.out.println(str);
	}
}

Output

Coding-Ninjas123

In this example, we have used the append function. This function will add anything at the end of the StringBuffer object passed as an argument.

Code 2

public class Ex22 {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer("Code");
		str.insert(2, "Ninj");
		System.out.println(str);
	}
}

Output

CoNinjde

In this code, we have shown the implementation of the insert method. This method will add something which is passed as an argument.

Code 3

public class Ex23 {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer("Ninjas-Coding");
		str.reverse();
		System.out.println(str);
	}
}

Output

gnidoC-sajniN

In this example, we have used the reverse method which the given String.

Code 4

public class Ex24 {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer("Hello Noob");
		str.replace( 6, 11, "Coder");
		System.out.println(str);
	}
}

Output

Hello Coder

In this example, we have used the replace function to replace a string from a particular index amount given in the argument list.

Code 5

public class Ex25 {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer();
		System.out.println( str.capacity() );
	}
}

Output

16

This function returns the default capacity of the StringBuffer object class.

Code 6

public class Ex26 {
	public static void main(String[] args) {
		StringBuffer str = new StringBuffer();
		System.out.println( str.capacity()); 
		str.ensureCapacity(30);
		System.out.println( str.capacity()); 
	}
}

Output

16
34

In this example, we have used the ensureCapacity function, which is used to increase the size of the StringBuffer object.

Code 7

public class Ex27 {
	public static void main(String[] args) {
		StringBuffer sb=new StringBuffer("Coding-Ninjas");  
		sb.delete(2,10);  
		System.out.println(sb);  
	}  
}  

Output

Cojas

In this example, we have used the delete function, which takes an integer as an input, and deleted the entire portion of the String from the start to the end of the String. Try this code by yourself on Online Java Compiler.

Related Article: String Args in Java

Frequently Asked Questions

  1.  Why do we use StringBuffer in Java? 
    StringBuffer is used to create modifiable String objects. This means that we can create StringBuffer to append, reverse, replace, concatenate and manipulate Strings. 
     
  2. Why is StringBuffer faster than String?
    StringBuffer is mutable as they can change their values. As String is immutable, another object gets created but not in StringBuffer if you try to change their values. 

Key Takeaways

In this blog, we have taken the topic of StringBuffer. We have given an introduction to StringBuffer and what is StringBuffer class is. We also talked about different constructors of StringBuffer along with syntax. We also provide a brief detail about various methods used in the StringBuffer class. We also took several code examples explaining every code function and its output. 

If you want to learn and practice more about these coding topics, feel free to visit our website, Interview Problems and you want to prepare for some specific company interview Bundles.

Also check out - String Interview Questions In Java

Recommended problems -

Previous article
Strings vs StringBuffer
Next article
StringBuilder Class in Java
Live masterclass