Table of contents
1.
Introduction
2.
Syntax of append() Method
2.1.
Parameters
2.2.
Return Value
3.
Examples
3.1.
Example 1: Using append() in StringBuilder
3.2.
Example 2: Using append() in StringBuffer
4.
Append() in StringBuilder and StringBuffer
4.1.
1. StringBuilder append()
4.1.1.
Example: Appending Different Data Types
4.2.
2. StringBuffer append()
4.2.1.
Example: Using append() in Multi-threaded Environment
5.
Frequently Asked Questions
5.1.
What is the difference between StringBuilder and StringBuffer in Java?
5.2.
Can we use append() with primitive data types?
5.3.
Does append() create a new string object?
6.
Conclusion
Last Updated: Mar 17, 2025
Medium

Append() Method in Java: StringBuilder and StringBuffer

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The append() method in Java is used to concatenate strings efficiently. It is available in both StringBuilder and StringBuffer classes, which are mutable alternatives to the immutable String class. The append() method allows adding text, numbers, characters, and other data types to an existing sequence without creating new objects. 

Append() Method in Java: StringBuilder and StringBuffer

In this article, we will discuss the append() method in StringBuilder and StringBuffer, along with examples demonstrating its usage.

Syntax of append() Method

The append() method is available in both StringBuilder and StringBuffer classes. The syntax is as follows:

public StringBuilder append(String str)

public StringBuffer append(String str)

Parameters

  • str: The string to be appended to the existing sequence.

Return Value

  • Returns the same StringBuilder or StringBuffer object after appending the specified string.

Examples

Example 1: Using append() in StringBuilder

public class AppendExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");
        sb.append(" World");
        System.out.println(sb);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Hello World


Explanation: Here, the append() method adds " World" to the existing string "Hello", modifying the StringBuilder object.

Example 2: Using append() in StringBuffer

public class AppendBufferExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Java");
        sb.append(" Programming");
        System.out.println(sb);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Java Programming


Explanation: The append() method efficiently modifies the StringBuffer object by adding " Programming" to " Java".

Append() in StringBuilder and StringBuffer

1. StringBuilder append()

The StringBuilder class is preferred when multiple string modifications are required because it is not thread-safe but faster compared to StringBuffer.

Example: Appending Different Data Types

public class StringBuilderExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Number: ");
        sb.append(100);
        sb.append(" is even.");
        System.out.println(sb);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Number: 100 is even.


Explanation: The append() method can add different types of data like integers and strings efficiently.

2. StringBuffer append()

The StringBuffer class is thread-safe but slightly slower compared to StringBuilder. It is used when multiple threads access the same object.

Example: Using append() in Multi-threaded Environment

public class StringBufferExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Multi-threaded: ");
        sb.append(Thread.currentThread().getName());
        System.out.println(sb);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Multi-threaded: main

 

Explanation: Here, append() is used in StringBuffer, which ensures safe execution in multi-threaded programs.

Frequently Asked Questions

What is the difference between StringBuilder and StringBuffer in Java?

StringBuilder is faster but not thread-safe, while StringBuffer is thread-safe but slightly slower. Both support the append() method.

Can we use append() with primitive data types?

Yes, append() supports primitive data types like int, double, char, etc., by converting them into strings before appending.

Does append() create a new string object?

No, append() modifies the existing StringBuilder or StringBuffer object, making it more memory-efficient compared to + concatenation.

Conclusion

The append() method in Java is used to add data at the end of an existing StringBuilder or StringBuffer object. Unlike String, which is immutable, both StringBuilder and StringBuffer allow modifications without creating new objects, making them more memory-efficient for string manipulations. The append() method can concatenate various data types like strings, numbers, and characters, ensuring flexibility in dynamic string operations.

Live masterclass