Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
StringJoiner add() method in Java
Next article icon

StringBuilder append() Method in Java

Last Updated : 15 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the append() method of StringBuilder class is used to add data to the end of an existing StringBuilder object. It supports appending different data types like strings, integers, characters, and booleans by making it easy for dynamic string manipulation.

Example 1: Here, we use the append() method to add a string to a StringBuilder object.

Java
// append() with strings public class Geeks {     public static void main(String[] args) {                // Create a StringBuilder object with          // an initial string         StringBuilder sb = new StringBuilder("Java");          // Append a string to the StringBuilder         sb.append(" Programming");          System.out.println(sb);     } } 

Output
Java Programming 

Now there are several versions of the append() method in StringBuilder and each version designed to append different types of data, such as strings, different data types, objects, or even subsequences.

Syntax of append() Method

public StringBuilder append(data)

  • Parameter: data: The value to append (can be any data type).
  • Return Type: StringBuilder: It returns the updated StringBuilder object.

Example 2: In this example, we use the append() method to add various data types to the StringBuilder object.

Java
// append() with multiple data types public class Geeks {     public static void main(String[] args) {                // Create a StringBuilder object         StringBuilder sb = new StringBuilder("The value is ");          // Append various data types         sb.append(21);                    sb.append(", ");                  sb.append(7.01);                   sb.append(", or ");               sb.append(false);                 System.out.println(sb);     } } 

Output
The value is 21, 7.01, or false 

Example 3: StringBuilder append(char[] str, int offset, int len)

This version appends a subarray of characters to the StringBuilder, starting at the specified offset and with the specified length.

Syntax:

public StringBuilder append(char[] str, int offset, int len)

Parameter:

  • char[] str: The character array that contains the characters to be appended.
  • int offset: The index in the character array where the subarray starts.
  • int len: The number of characters to append from the array starting from the offset.

Return Type: StringBuilder: The method returns the same StringBuilder instance with the appended characters.

Java
// append() with character arrays public class Geeks {     public static void main(String[] args) {                // Create a StringBuilder object         StringBuilder sb = new StringBuilder("Programming ");          // Create a character array         char[] ch = {'i', 'n', ' ', 'J', 'a', 'v', 'a'};          // Append part of the character array         sb.append(ch, 0, 4);   // Appends "in J"          System.out.println(sb);     } } 

Output
Programming in J 

Example 4: StringBuilder append(CharSequence chseq, int start, int end)

This version appends a subsequence of characters from a CharSequence to the StringBuilder by using specified start and end indices.

Syntax:

public StringBuilder append(CharSequence chseq, int start, int end)

Parameter:

  • CharSequence chseq: The CharSequence like String, StringBuilder, etc. from which the subsequence is appended.
  • int start: The starting index of the subsequence.
  • int end: The ending index of the subsequence (exclusive).

Return Type:

  • StringBuilder: The method returns the same StringBuilder instance with the appended subsequence.
Java
// append() with CharSequence public class Geeks {     public static void main(String[] args) {                StringBuilder sb = new StringBuilder("Welcome to ");         CharSequence ch = "JavaProgramming";          // Appends 'Programming' from the CharSequence         sb.append(ch, 5, 14);         System.out.println(sb);      } } 

Output
Welcome to rogrammin 

Example 5: StringBuilder append(Object obj)

This version appends the string representation of an object to the StringBuilder.

Syntax:

public StringBuilder append(Object obj)

  • Parameter: Object obj: The object whose string representation will be appended to the StringBuilder.
  • Return Type: StringBuilder: The method returns the same StringBuilder instance with the appended string representation of the object.
Java
// append() with Object public class GFG {        public static void main(String[] args) {                StringBuilder sb = new StringBuilder("");         Object o = 21;                // Appends the string representation          // of the object         sb.append(o);          System.out.println(sb);      } } 

Output
21 

Example 6: This example demonstrates chaining multiple append() calls.

Java
// chaining append() calls public class Geeks {     public static void main(String[] args) {                // Create a StringBuilder object         StringBuilder sb = new StringBuilder();          // Chain multiple append() calls         sb.append("Java").append(" is a ").append("Programming Language.");          System.out.println(sb);      } } 

Output
Java is a Programming Language. 



Next Article
StringJoiner add() method in Java
author
ankita_chowrasia
Improve
Article Tags :
  • Java
  • Java-StringBuilder
Practice Tags :
  • Java

Similar Reads

  • StringBuilder capacity() Method in Java
    In Java, the capacity() method of StringBuilder Class is used to return the current capacity of StringBuilder object. The capacity is the amount of storage available to insert new characters. Example 1: Here, we use the capacity() method to check the current capacity of the StringBuilder object. [GF
    2 min read
  • StringJoiner add() method in Java
    The add(CharSequence newElement) of StringJoiner adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null, then "null" is added.Syntax: public StringJoiner add(CharSequence newElement) Parameters: This method takes a mandatory parameter newElem
    1 min read
  • StringBuffer append() Method in Java with Examples
    Pre-requisite: StringBuffer class in Java The java.lang.StringBuffer.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used: StringBuffer append(boolean a) :The java.lang.StringBuffer.append(boole
    13 min read
  • StringBuilder appendCodePoint() method in Java with Examples
    The appendCodePoint(int codePoint) method of StringBuilder class is the inbuilt method used to append the string representation of the codePoint argument to this sequence. The argument is appended to this StringBuilder content and length of the object is increased by Character.charCount(codePoint).
    2 min read
  • StringBuilder length() in Java with Examples
    The length() method of StringBuilder class returns the number of character the StringBuilder object contains. The length of the sequence of characters currently represented by this StringBuilder object is returned by this method. Syntax: public int length() Return Value: This method returns length o
    2 min read
  • Java StringBuilder delete(int start, int end) Method
    delete(int start, int end) method in the StringBuilder class is used to remove a portion of the string, starting from the specified start index to the specified end index. This method is used to modify mutable sequences of characters. Example 1: The below example demonstrates how to use the delete()
    2 min read
  • StringBuilder toString() method in Java with Examples
    The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString(
    3 min read
  • Matcher appendTail(StringBuilder) method in Java with Examples
    The appendTail(StringBuilder) method of Matcher Class behaves as a append-and-replace method. This method reads the input string and appends it to the given StringBuilder at the tail position. Syntax: public StringBuilder appendTail(StringBuilder builder) Parameters: This method takes a parameter bu
    2 min read
  • StringBuilder trimToSize() method in Java with Examples
    The trimToSize() method of StringBuilder class is the inbuilt method used to trims the capacity used for the character sequence of StringBuilder object. If the buffer used by StringBuilder object is larger than necessary to hold its current sequence of characters, then this method is called to resiz
    2 min read
  • StringBuilder setLength() in Java with Examples
    The setLength(int newLength) method of StringBuilder is used to set the length of the character sequence equal to newLength.For every index k greater than 0 and less than newLength. If the newLength passed as argument is less than the old length, the old length is changed to the newLength.If the new
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences