A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/stringbuilder-append-method-in-java-with-examples/ below:

StringBuilder append() Method in Java

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);
    }
}

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)

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:

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);
    }
}

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:

Return Type:

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)

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); 
    }
}

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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4