0% found this document useful (0 votes)
24 views5 pages

String Buffer String Builder

The Java StringBuffer class creates mutable String objects and is thread-safe, allowing for modifications without simultaneous access issues. It includes methods for appending, inserting, replacing, deleting, reversing, and managing capacity. In contrast, the StringBuilder class is similar but non-synchronized, making it more efficient, while both differ from the immutable String class.

Uploaded by

akshayt0011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

String Buffer String Builder

The Java StringBuffer class creates mutable String objects and is thread-safe, allowing for modifications without simultaneous access issues. It includes methods for appending, inserting, replacing, deleting, reversing, and managing capacity. In contrast, the StringBuilder class is similar but non-synchronized, making it more efficient, while both differ from the immutable String class.

Uploaded by

akshayt0011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java StringBuffer Class

Java StringBuffer class is used to create mutable (modifiable) String objects. The
StringBuffer class in Java is the same as String class except it is mutable i.e. it can be
changed.
Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously.
So it is safe and will result in an order.
What is a mutable String?
A String that can be modified or changed is known as mutable String. StringBuffer and
StringBuilder classes are used for creating mutable strings.

1) StringBuffer Class append() Method


The append() method concatenates the given argument with this String.

2) StringBuffer insert() Method

The insert() method inserts the given String with this string at the given position.

3) StringBuffer replace() Method

The replace() method replaces the given String from the specified beginIndex and
endIndex.

4) StringBuffer delete() Method

The delete() method of the StringBuffer class deletes the String from the specified
beginIndex to endIndex.

5) StringBuffer reverse() Method

The reverse() method of the StringBuilder class reverses the current String.

6) StringBuffer capacity() Method

The capacity() method of the StringBuffer class returns the current capacity of the buffer.
The default capacity of the buffer is 16. If the number of character increases from its
current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your
current capacity is 16, it will be (16*2)+2=34.

7) StringBuffer ensureCapacity() method

The ensureCapacity() method of the StringBuffer class ensures that the given capacity is
the minimum to the current capacity. If it is greater than the current capacity, it increases
the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be
(16*2)+2=34.

Java StringBuilder Class

Java StringBuilder class is used to create mutable (modifiable) String. The Java
StringBuilder class is same as StringBuffer class except that it is non-synchronized. It is
available since JDK 1.5.

1) StringBuilder append() method

The StringBuilder append() method concatenates the given argument with this String.

2) StringBuilder insert() method

The StringBuilder insert() method inserts the given string with this string at the given
position.

3) StringBuilder replace() method

The StringBuilder replace() method replaces the given string from the specified
beginIndex and endIndex.

4) StringBuilder delete() method

The delete() method of StringBuilder class deletes the string from the specified
beginIndex to endIndex.
5) StringBuilder reverse() method

The reverse() method of StringBuilder class reverses the current string.

6) StringBuilder capacity() method

The capacity() method of StringBuilder class returns the current capacity of the Builder.
The default capacity of the Builder is 16. If the number of character increases from its
current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your
current capacity is 16, it will be (16*2)+2=34.

7) StringBuilder ensureCapacity() method

The ensureCapacity() method of StringBuilder class ensures that the given capacity is the
minimum to the current capacity. If it is greater than the current capacity, it increases the
capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be
(16*2)+2=34.

Difference between String and StringBuffer

There are many differences between String and StringBuffer. A list of differences between
String and StringBuffer are given below:

No. String StringBuffer

1) String class is immutable. StringBuffer class is mutable.

2) String is slow and consumes more memory when you StringBuffer is fast and consumes
concat too many strings because every time it creates less memory when you cancat
new instance. strings.

3) String class overrides the equals() method of Object StringBuffer class doesn't override
class. So you can compare the contents of two strings the equals() method of Object class.
by equals() method.
Difference between StringBuffer and StringBuilder

Java provides three classes to represent a sequence of characters: String, StringBuffer,


and StringBuilder. The String class is an immutable class whereas StringBuffer and
StringBuilder classes are mutable. There are many differences between StringBuffer and
StringBuilder. The StringBuilder class is introduced since JDK 1.5.

A list of differences between StringBuffer and StringBuilder are given below:

No. StringBuffer StringBuilder

1) StringBuffer is synchronized i.e. thread safe. StringBuilder is non-synchronized i.e. not


It means two threads can't call the methods thread safe. It means two threads can call the
of StringBuffer simultaneously. methods of StringBuilder simultaneously.

2) StringBuffer is less efficient than StringBuilder is more efficient than


StringBuilder. StringBuffer.

You might also like