GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — StringBuilder append() Method in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

StringBuilder append() Method in Java

Last Updated : 11 Jul, 2025

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.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.