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);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Coding-Ninjas123

You can also try this code with Online Java Compiler
Run Code
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);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
CoNinjde

You can also try this code with Online Java Compiler
Run Code
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);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
gnidoC-sajniN

You can also try this code with Online Java Compiler
Run Code
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);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Hello Coder

You can also try this code with Online Java Compiler
Run Code
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() );
}
}

You can also try this code with Online Java Compiler
Run Code
Output
16

You can also try this code with Online Java Compiler
Run Code
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());
}
}

You can also try this code with Online Java Compiler
Run Code
Output
16
34

You can also try this code with Online Java Compiler
Run Code
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);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Cojas

You can also try this code with Online Java Compiler
Run Code
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
-
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.
-
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 -