Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

StringBuffer Class in Java

Updated on 02 June, 2023

5.52K+ views
9 min read

Basic Concept of Class in Java

The basic concepts of any object-oriented programming language are the classes and objects which spin around various practical and real-life entities. A class in Java is a user-defined template or prototype or blueprint from which the objects are developed. It denotes the set of methods or properties that are common to all objects of the same data type. The declaration of a class includes the below-mentioned components in the same order.

  • Modifiers: A class may have default access or can be public.
  • class Keyword: The keyword ‘class’ is used for class creation.
  • Class name: The name of a class should start with an initial letter that is capitalized by convention.
  • Superclass (if any): It is the name of the parent class if the class to be declared is a derived class. The name of the superclass is preceded by the ‘extend’ keyword. Any class in Java can extend only one parent. 
  • Interfaces (if any): If the class has interfaces, then a list of interfaces incorporated by the class with a comma separation between the elements of the list should be mentioned. The list is preceded by the ‘implements’ keyword. More than one interface can be implemented by a class.
  • Body: The body of a class is enclosed within the braces { }.

Check out our free technology courses to get an edge over the competition.

An Overview of String Buffer in Java

The StringBuffer in Java is a class used to generate changeable objects of String type. A StringBuffer can be incorporated to reverse, append, concatenate, replace and manipulate a sequence of characters or strings. Respective methods of the StringBuffer class are created to accomplish these functions in a Java program. The strings in Java are non-editable and hence are referred to as immutable types. It can be changed only by overwriting an existing string. StringBuffer can store the character sequences while it can enable the mutation of the string. 

The StringBuffer class in Java features the flexibility of memory allocation of the character sequence. This implies that a string can be modified with the help of the StringBuffer class. The contingent memory reserved for future modification that exists in the StringBuffer class is the principal element of this modification.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career. 

Important points about the StringBuffer class in Java

  • The Java StringBuffer is much like the String class with the features of mutability. 
  • The memory allocation and length of a string are final, whereas there are existing methods in the StringBuffer class to manipulate these string properties. 
  • Most of the methods in StringBuffer are synchronized and hence it is a thread safe class because the constituent objects cannot be fetched or utilized by several threads at the same instance of time. 
  • The abstract class AbstractStringBuilder is extended by the java.lang.StringBuffer.
  • From the superclass java.lang.Object, the String buffer inherits equals(), clone(), finalize(), hashCode(), notify(), notifyAll() and getClass() methods. 
  • The Appendable, Serializable and CharSequence interfaces are implemented by the StringBuffer. 
  • The capacity() method of the StringBuffer is used to regain the memory existing for new sequences of characters to be added. The allocation will occur beyond this.
  • Because of its time synchronization, the StringBuffer class performs slower than the StringBuilder class.

The figure below shows the diagrammatic representation of the class The figure below shows the diagrammatic representation of the class StringBuffer in Java..

StringBuffer Constructors

As mentioned earlier, the StringBuffer class is used to generate mutable sequences of characters or objects of string type. In other words, the string can be altered even after it is completely created. This denotes a writable and growable character sequence. So, when a programmer has the need of making frequent modifications in the input string, it is recommended that the StringBuffer class is used. The StringBuffer class defines four different constructors discussed below.

  1. StringBuffer()
  2. StringBuffer(int sizeOfBuffer)
  3. StringBuffer(String str)
  4. StringBuffer(charSequence []ch)

StringBuffer()

This constructor is used to create a StringBuffer with empty or null content. It has sixteen reserved spaces for the characters by default.

Example: StringBuffer sb = new StringBuffer();

StringBuffer(int sizeOfBuffer)

This constructor creates an empty string and accepts an integer argument that specifies the size of the newly created empty buffer.

Example: StringBuffer sb = new StringBuffer(20);

StringBuffer(String string)

This constructor creates a StringBuffer that accepts an input argument of string type as the initial buffer content. There exists a pre-allocated set of 16 contingent memory locations for the modification of input string if required. This pre-allocated space does not include the buffer memory spaces.

Example: StringBuffer sb = new StringBuffer(“Hello World!”);

StringBuffer(charSequence [ ]ch)

With this constructor, a StringBuffer object is created from the array of charSequence.

Methods of StringBuffer class

The StringBuffer class contains several methods that are discussed in this section with examples. 

length():

This method returns the length of the object in the StringBuffer class.

capacity():

This method is invoked to return the capacity of the object in the StringBuffer class. 

The output of the above code is as follows:

append():

This method is invoked to affix the specified parameter representing a string at the end of the existing StringBuffer. We overload this method for all the primitive objects and data types. 

The output of the above code is as follows:

Hello World 2017

insert():

This method accepts two input parameters –a value to be inserted and an integer value representing the index at which the value is to be inserted in the string. The index specifies the StringBuffer where to add the input parameter of the character sequence. We overload this method to handle primitive objects and data types.

The output of the above code is as follows.

Hello World 2017

reverse():

This function enables the buffer to reverse the existing string or the contents of the character sequence in the buffer. The reversed string is returned. If the object does not contain an existing string or a character sequence, the reverse() function throws a NullPointerException.

The output of the above code is mentioned below:

dlroW olleH

delete(int startIndex, int endIndex)

This method takes two input arguments. The first parameter serves as the index of the start of the deletion and the second one denotes the index of the end of the deletion. Both the input parameters are integer values. Therefore, the sequence of characters between the start and end index is deleted. The function returns the remaining content of the string buffer.

deleteCharAt(int index)

This function is used to delete a single character from the contents of the string buffer. It takes a single input parameter of integer type. The integer value determines the index of the value to be deleted from the character sequence in the buffer. This method returns the remaining content after deletion of the single character from the existing contents of the buffer.

replace(int startIndex, int endIndex, String str)

It is used to replace the specified portion of a character sequence with another string. This method in the StringBuffer class accepts three input parameters. The first two parameters are of integer type and denote the start and end index of the existing string buffer contents to be replaced. The third input parameter is string type. This is the character sequence that replaces the character sequence in the existing string buffer contents from the specified start index. 

ensureCapacity()

This method can be used to assure the least capacity of the object in the StringBuffer class. If the input argument of this method is less than the allocated memory’s existing capacity, then no change will happen in the existing capacity. However, if the input parameter is more than the existing allocated memory’s capacity, the current capacity of the allocated memory is altered, as mentioned below.

newCapacity = (oldCapacity*2) + 2 

Differences between String and StringBuffer in Java

StringBuffer String
This class is a mutable class in Java. The String class is non-changeable or immutable.
Its execution is faster and less memory is consumed when strings are concatenated. Its operation is slow and more memory is consumed when many strings are concatenated.
The StringBuffer class does not override the equals( ) method of the object class.  The equals() method of the object class is overridden by the String class. So, the comparison of two strings can be performed using the equals() functions.

At upGrad, we understand the importance of practical, hands-on learning – especially when it comes to software development. As a result, our courses and training initiatives have practicality at their very core. One such initiative is Full Stack Development Bootcamp which will help you develop all the relevant skills required to excel in full-stack development. 

RELATED PROGRAMS