Java StringBuffer append() Method

The StringBuffer.append() method in Java is used to append various types of data to the existing sequence of characters in a StringBuffer object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. append Method Syntax
  3. Examples
    • Appending Strings
    • Appending Different Data Types
    • Chaining append Methods
  4. Conclusion

Introduction

The StringBuffer.append() method is a member of the StringBuffer class in Java. It allows you to efficiently append different types of data to a StringBuffer object without creating a new object for each modification. Unlike StringBuilder, StringBuffer is synchronized, making it thread-safe and a preferred choice for string manipulation in multi-threaded environments.

append Method Syntax

The syntax for the append method is as follows:

public synchronized StringBuffer append(String str)

The append method is overloaded to accept different data types:

  • StringBuffer append(boolean b)
  • StringBuffer append(char c)
  • StringBuffer append(char[] str)
  • StringBuffer append(char[] str, int offset, int len)
  • StringBuffer append(double d)
  • StringBuffer append(float f)
  • StringBuffer append(int i)
  • StringBuffer append(long lng)
  • StringBuffer append(CharSequence s)
  • StringBuffer append(CharSequence s, int start, int end)
  • StringBuffer append(Object obj)
  • StringBuffer append(String str)
  • StringBuffer append(StringBuffer sb)

Examples

Appending Strings

The append method can be used to concatenate strings to the existing sequence in a StringBuffer object.

Example

public class StringBufferAppendExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with initial content "Hello"
        StringBuffer sb = new StringBuffer("Hello");

        // Append ", World!" to the existing sequence
        sb.append(", World!");

        // Print the final content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

Hello, World!

Appending Different Data Types

The append method is overloaded to handle different data types, allowing you to append integers, characters, booleans, and other types directly.

Example

public class StringBufferAppendExample {
    public static void main(String[] args) {
        // Create an empty StringBuffer object
        StringBuffer sb = new StringBuffer();

        // Append different types of data to the StringBuffer
        sb.append("Age: ");    // Append a string
        sb.append(30);         // Append an integer
        sb.append(", Height: ");
        sb.append(5.9);        // Append a double
        sb.append(", Active: ");
        sb.append(true);       // Append a boolean

        // Print the final content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

Age: 30, Height: 5.9, Active: true

Chaining append Methods

The append method returns the StringBuffer object itself, allowing for method chaining to perform multiple append operations in a single statement.

Example

public class StringBufferAppendExample {
    public static void main(String[] args) {
        // Create an empty StringBuffer object
        StringBuffer sb = new StringBuffer();

        // Chain multiple append operations
        sb.append("Name: ").append("Ramesh").append(", Occupation: ").append("Developer");

        // Print the final content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

Name: Ramesh, Occupation: Developer

Conclusion

The StringBuffer.append() method in Java is a versatile and efficient way to build and modify strings dynamically. By understanding how to use this method, you can optimize your string manipulation operations, especially in scenarios where performance and thread safety are crucial. Whether you need to append strings, handle different data types, or chain multiple append operations, the append method provides a powerful solution for these tasks.

Comments