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

16 June 2022

This document compares and contrasts the String, StringBuffer, and StringBuilder classes in Java. It provides methods and descriptions for StringBuffer and lists some key differences between the three classes. Specifically, it notes that String is immutable, StringBuffer is synchronized and thread-safe, and StringBuilder is mutable but not synchronized.

Uploaded by

sandesh ahir
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)
9 views5 pages

16 June 2022

This document compares and contrasts the String, StringBuffer, and StringBuilder classes in Java. It provides methods and descriptions for StringBuffer and lists some key differences between the three classes. Specifically, it notes that String is immutable, StringBuffer is synchronized and thread-safe, and StringBuilder is mutable but not synchronized.

Uploaded by

sandesh ahir
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

Method Decription

int lastIndexOf(Char ch); returns index of last occurrence of the


specified character if the specified
character is not available then return
-1.

String Buffer:
• If a user wants to change the content frequently then it is
recommended to go for StringBuffer.
• StringBuffer objects are mutable, so they can be modified. • We
can create a StringBuffer object by using new operator and pass the
string to the object, as:
StringBuffer sb = new StringBuffer (“Sujatha");
• The default initial capacity of a StringBuffer is"16". • After
reaching its maximum limit it will be increased to
(currentcapacity+1)*2. ie; (16+1)*2.

StringBuffer Methods
Method Description

int length() Return the no of characters present in the StringBuffer


int capacity() Returns how many characters a StringBuffer can hold
char charAt(int index) Returns the character located at specified index.
void setCharAt(int index, char ch) Replaces the character locating at
specified index with the provided
character.
delete(int begin,int end) Deletes characters from begin index to end
n-1 index.
deleteCharAt(int index) Deletes the character locating at specified
index
reverse() Reverses the given StringBuffer

Method Description

void setLength(int length) Consider only specified no of


characters and remove all the
remaining characters
void ensureCapacity(int To increase the capacity
initialcapacity); dynamically based on our
requirement.

StringBuilder
⚫ String Builder will be having same methods as
of StringBuffer except the following differences.
StringBuffer StringBuilder

Every method present in No method present in StringBuilder


StringBuffer is synchronized. is synchronized.

At a time only one thread is allowed At a time Multiple Threads are


to operate on the StringBuffer object allowed to operate simultaneously
hence StringBuffer object is Thread on the StringBuilder object hence
safe. StringBuilder is not Thread safe.
It increases waiting time of the Threads are not required to wait and
Thread and hence relatively hence relatively performance is
performance is low. high.
Introduced in 1.0 version. Introduced in 1.5 versions.
Differences b/w String, StringBuffer & String Builder

➢ String is immutable while StringBuffer and StringBuilder is mutable

object. ➢ StringBuffer is synchronized while StringBuilder is not which

makes StringBuilder faster than StringBuffer.

➢ Use String if you require immutability use Stringbuffer in java if you need

mutable + thread-safety and use StringBuilder in Java if you require mutable +

without thread-safety.

You might also like