tutorialcup
StringBuffer Java - StringBuffer class and methods with examples
StringBuffer class in Java
The StringBuffer in Java is a class that we can use to manipulate Strings. Strings are immutable which means it is of fixed length whereas StringBuffer is mutable and growable meaning we can change the length of string and do different manipulations like append, delete, insert, etc. In this tutorial, we will discuss the StringBuffer class in Java and methods in detail with examples. We will also see the difference between StringBuffer vs StringBuilder towards the end of the tutorial.
Features
Below are the features of the StringBuffer class:
- It creates a mutable String object
- It is thread-safe and synchronized which means we can use it for multithreading.
- Implements the CharSequence, Appendable, and Serializable interfaces.
- Inherits all the methods from the Object class.
Constructors
The StringBuffer class contains the below constructors:
StringBuffer methods in Java
Below are the StringBuffer methods
StringBuffer examples
Let's see various examples by using different StringBuffer methods.
Example: insert() method - Java StringBuffer class
We can use the insert() method to insert a new string at the required index position. In the below example, it inserts the new string at position 4 hence prints the output as "Javalanguage".
public class StringBufferDemo {
public static void main(String args) {
StringBuffer s = new StringBuffer("Java");
s.insert(4, "language");
System.out.println(s);
StringBuffer Java - StringBuffer class and methods with examples
StringBuffer class in Java
The StringBuffer in Java is a class that we can use to manipulate Strings. Strings are immutable which means it is of fixed length whereas StringBuffer is mutable and growable meaning we can change the length of string and do different manipulations like append, delete, insert, etc. In this tutorial, we will discuss the StringBuffer class in Java and methods in detail with examples. We will also see the difference between StringBuffer vs StringBuilder towards the end of the tutorial.
Features
Below are the features of the StringBuffer class:
- It creates a mutable String object
- It is thread-safe and synchronized which means we can use it for multithreading.
- Implements the CharSequence, Appendable, and Serializable interfaces.
- Inherits all the methods from the Object class.
Constructors
The StringBuffer class contains the below constructors:
StringBuffer methods in Java
Below are the StringBuffer methods
StringBuffer examples
Let's see various examples by using different StringBuffer methods.
Example: insert() method - Java StringBuffer class
We can use the insert() method to insert a new string at the required index position. In the below example, it inserts the new string at position 4 hence prints the output as "Javalanguage".
public class StringBufferDemo {
public static void main(String args) {
StringBuffer s = new StringBuffer("Java");
s.insert(4, "language");
System.out.println(s);