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

What is Append In Java? & its Implementation in StringBuilder and StringBuffer Classes

Updated on 18 November, 2024

43.47K+ views
8 min read

In my experience as a software developer, handling strings in Java is a frequent requirement. However, Java strings are immutable, meaning they cannot be altered once created. To work around this limitation, I often turn to two classes: StringBuffer and StringBuilder. These classes provide various methods, including insert, replace, delete, and Append in Java. 

The append method is particularly useful for adding data to a file. It allows me to seamlessly incorporate characters, Boolean values, strings, integers, floats, and more into my programs. Mastering the append method in Java has been crucial for efficiently managing string manipulation tasks ensuring smooth integration of new data into existing files or structures. 

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

Append in Java

Append in Java is a StringBuilder and StringBuffer class method used to append a value to the current sequence. String concatenation in Java is done using the StringBuilder or StringBuffer class and append() method.

String Classes in Java

String class is final and has no child classes, and its instances cannot be modified after creation. The StringBuilder in Java represents a mutable sequence. The StringBuilder class provides an alternative to String Class.

Check out upGrad’s Java Bootcamp

Source

Due to immutability, new string instances are created after each operation, and old ones are discarded, creating lots of garbage. Thus, StringBuffer or StringBuilder classes deal with the temporary garbage generation due to modifications to the String.

The function of StringBuilder is quite similar to the StringBuffer class. However, both the StringBuilder and StringBuffer class differ in synchronisation. The StringBuffer class is synchronised, whereas the StringBuilder class provides no synchronisation. Hence, StringBuffer will be modified frequently in a multi-threaded environment and StringBuilder in a single-threaded environment.

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)

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.

Append Method Implementation in StringBuilder and StringBuffer Classes

Append in Java is StringBuilder and StringBuffer classes’ method that adds a new value to the current sequence. The appending can be done in 13 forms. 

Syntax

public StringBuffer append()

Let’s see the actual implementations of the append method in all the forms:

1. StringBuffer append(boolean bo): Appending string to the boolean argument.

import java.lang.*;  

public class AB {

    public static void main(String[] args)

    {

        StringBuffer sbd = new StringBuffer ("This is appending boolean ");

        System.out.println("Input: " + sbd);

        sbd.append(in Java);

       System.out.println("Output: "+ sbd);

    }

}

Output

Input: This is appending boolean

Output: This is appending boolean in Java

Our learners also read: Java free online courses!

2. StringBuffer append(char ch): Appending string to the character argument.

import java.lang.*; 

public class CD{

    public static void main(String[] args)

    {

        StringBuffer sbd = new StringBuffer("This is appending character");

     sbf.append('A');

        System.out.println("After appending = " + sbd);

  }

}

Output:

This is appending a character

After appending = This is appending character A

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

3. StringBuffer append(char[] string):  Appending string to the character array.

import java.lang.*;

public class EF{  

    public static void main(String[] args)

{

        StringBuffer sbd = new StringBuffer(" This is appending a character string ");

        char[] astr = new char[] { 'I', 'N', 'D', 'I', 'A' };

        sbd.append(astr);

        System.out.println("After appending = " + sbd);

    }

} 

Output:

This is appending a character string

After appending = This is appending a character string INDIA

4. StringBuffer append(char[] string, int offset, int len): Appending string to a character array.

import java.lang.*;  

public class GH {  

    public static void main(String[] args)

    {   

        StringBuffer sbd = new StringBuffer ("Java");

        System.out.println(" Before Appending= " + sbd);  

        char[] cstr = new char[] { 'i', 's', 'a', 'w', 'e', 's', 'o', 'm', 'e' };

        sb.append(cstr, 0, 3);   

        System.out.println("After appending string = " + sbd);

    }

}

Output:

Before Appending= Java

After appending string = Javaisawesome

5. StringBuffer append(CharSequence cse): Appending string to a character Sequence.

import java.lang.*;  

public class IJ {

    public static void main(String[] args)

    {

         StringBuffer sbd = new StringBuffer("Javais");

        System.out.println(" string = " + sbd);

        CharSequence chSeq = "Awesome";   

        sbf.append(chSeq);

        System.out.println("After appending = " + sbd);

    }

}

Output:

string = Javais

After appending = JavaisAwesome

6. StringBuffer append(CharSequence cse, int start, int end): Appending Character Sequence to a String.

import java.lang.*;  

public class KL { 

public static void main(String[] args)

{

     StringBuffer sbd = new StringBuffer("Java is  ");

        System.out.println(" string = " + sbd);   

        CharSequence chSeq = "awesome";

        sbf.append(chSeq, 1, 4);  

        System.out.println("After appending string = " + sbf);

    }

}

Output:

string = Java is

After appending string = Java is weso

7. StringBuffer append(double do): Appending a double to the string.

Program:

import java.lang.*;

public class MN {  

    public static void main(String[] args)

    {  

        StringBuffer sbd = new StringBuffer(“The main number is: “);

        Double astr = new Double(522.39);

        sbd.append(astr);

        System.out.println(“After appending = ” + sbd);

    }

}

Output:

The main number is:

After appending = 522.39

8. StringBuffer append(float fl): Appending the string to float.

import java.lang.*;  

public class OP {

    public static void main(String[] args)

    {  

        StringBuffer sbd = new StringBuffer(“Java is awesome “);

        Float astr = new Float(1.23);

        sbd.append(astr);

        System.out.println(“After appending = ” + sbd);

    }

}

Output:

Java is awesome

After appending = Java is awesome 1.23

9. StringBuffer append(int i): Append integer to the string.

import java.lang.*;  

public class QR {

    public static void main(String[] args)

    {

        StringBuffer sbd = new StringBuffer(“Java is awesome “);

        Integer astr = new Integer(478);

        sbd.append(astr);

        System.out.println(“After appending = ” + sbd);

    }

} 

Output:

Java is

After appending = Java is awesome 478

10. StringBuffer append(long lng): Appending the string to a long argument.

import java.lang.*;  

public class ST {  

    public static void main(String[] args)

    {

        StringBuffer sbd = new StringBuffer("Java is Awesome ");  

        Long astr = new Long(1993);

        sbd.append(astr);

        System.out.println("After appending = " + sbd);

    }

}

Output:

Java is Awesome

After appending  = Java is Awesome 1993

11. StringBuffer append(Object obj): Appending an Object to a string.

import java.lang.*;  

public class UV{  

    public static void main(String[] args)

    {  

        StringBuffer sbd = new StringBuffer("Javais");

        System.out.println("string = " + sbd);  

        Object objectvalue = "awesome";  

        sbd.append(objectvalue);  

        System.out.println("After appending = " + sbd);

    }

}

Output:

string = Javais

After appending = Javaisawesome

12. StringBuffer append(String str): Append a string to another string.

import java.lang.*;  

public class WX{  

    public static void main(String[] args)

    {

        StringBuffer sbd = new StringBuffer("Javais");

        System.out.println("string = " + sbd);   

        String strvalue = "awesome";

        sbd.append(strvalue);   

        System.out.println("After appending = " + sbd);

    }

}

Output:

string = Javais

After appending = Javaisawesome

13. StringBuffer append(StringBuilder sbf): Appending the StringBuilder to StringBuffer

import java.lang.*;

public class YZ{  

    public static void main(String[] args)

    {

        StringBuffer sbd = new StringBuffer("Javais");

        System.out.println("String 1 = " + sbd1);

        StringBuffer sbf2 = new StringBuilder("awesome ");

        System.out.println("String 2 = " + sbd2);

        Sbd1.append(sbd2);  

        System.out.println("After appending = " + sbd1);

    }

}

Output:

String 1 = Javais

String 2 = awesome

After appending = Javaisawesome

Conclusion

As I conclude our exploration of append in Java, you will better understand their significance in programming. Learning how to append data using StringBuilder and StringBuffer classes has been enlightening. 

This skill empowers you to enhance coding, making it more readable, maintainable, and efficient. With the ability to append, you can seamlessly handle tasks such as string concatenation and file manipulation in Java applications. 

This newfound proficiency in append equips me to tackle more intricate programming challenges and develop robust software solutions. Given Java’s widespread use, refining my append skills is essential for advancing in my career and staying competitive in the industry. 

Therefore, I believe you will be committed to further practicing and refining the knowledge of append in Java, leveraging it to create even more effective programs and propel in professional growth. 

upGrad offers a Unique Master of Science in Computer Science Course for honing your skills and fostering growth in your software development career journey.

Check out all the Trending Java Tutorial Topics in 2024

Frequently Asked Questions (FAQs)

1. What is StringBuffer and StringBuilder class in Java?

Java StringBuffer class is used to create modifiable or mutable objects. This class is similar to the string class except for a couple of differences. StringBuffer(), StringBuffer(String str), and StringBuffer(int capacity) are the important constructors of this class. StringBuffer() constructor allocates a capacity of 16 to an empty string buffer. StringBuffer(String str) assigns a specified string to a string buffer. StringBuffer(int capacity) specifies the length of the buffer and creates an empty string buffer. StringBuilder class focuses on creating modifiable or mutable strings. A StringBuffer is not synchronized. StringBuilder(), StringBuilder(String str), and StringBuilder(int length) are important constructors of this class. The StringBuilder() constructor allocates a capacity of 16 to an empty string builder. StringBuilder(String str) assigns a specified string to a string. StringBuilder(int length) specifies the length of the buffer and creates an empty string buffer.

2. How are String and StringBuffer different from each other?

The String class is immutable whereas the StringBuffer class is mutable. The next difference between them is that String class works with a string constant pool and StringBuffer uses heap memory. Performing concatenation operations using a string class is a slow process compared to the StringBuffer class. Due to the slowness of the string class, it consumes more memory. However, StringBuffer works with less memory due to its speed.

3. What is the difference between StringBuffer and StringBuilder?

Java uses String, StringBuffer, and StringBuilder classes for its characters. The notable differences between the two starts with their introduction. StringBuffer was used during Java 1.0 whereas StringBuilder’s introduction happened in Java 1.5. The efficiency of StringBuffer is less than StringBuilder. Moreover, StringBuffer is thread-safe which means it is synchronized. This means two threads can’t call StringBuffer methods at the same time. StringBuilder isn’t synchronized and isn’t thread-safe. Therefore, two threads can refer to the StringBuilder methods at the same time. Due to these differences, depending on the scenarios, one class object is used over the other.

RELATED PROGRAMS