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.