VOOZH about

URL: https://www.geeksforgeeks.org/java/stringbuilder-append-method-in-java-with-examples/

⇱ StringBuilder append() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.


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.


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.


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.

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.

Output
21

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


Output
Java is a Programming Language.
Comment