Java StringBuilder append() Method Example

1. Introduction

The StringBuilder class in Java is essential for constructing strings efficiently. It provides numerous append() methods to accommodate various data types, allowing for dynamic string building without generating multiple intermediate objects. This tutorial explores these methods through practical examples.

Key Points

1. StringBuilder append(boolean b) - Appends the string representation of the boolean.

2. StringBuilder append(char c) - Appends the char value.

3. StringBuilder append(char[] str) - Appends the complete char array.

4. StringBuilder append(char[] str, int offset, int len) - Appends a subarray of chars.

5. StringBuilder append(CharSequence s) - Appends the CharSequence.

6. StringBuilder append(CharSequence s, int start, int end) - Appends a subsequence of the CharSequence.

7. StringBuilder append(double d) - Appends the double value.

8. StringBuilder append(float f) - Appends the float value.

9. StringBuilder append(int i) - Appends the int value.

10. StringBuilder append(long lng) - Appends the long value.

11. StringBuilder append(Object obj) - Appends the string representation of the Object.

12. StringBuilder append(String str) - Appends the String.

13. StringBuilder append(StringBuffer sb) - Appends the StringBuffer.

14. StringBuilder appendCodePoint(int codePoint) - Appends the codePoint as a character.

2. Program Steps

Step 1: Initialize a new StringBuilder instance.

Step 2: Append various data types using the append() method.

Step 3: Output the results of the append operations.

3. Code Program

public class AppendExample {
    public static void main(String[] args) {
        StringBuilder builder;

        // Append String
        builder = new StringBuilder().append("guides");
        System.out.println("Append string : " + builder);

        // Append char
        builder = new StringBuilder().append('c');
        System.out.println("Append char : " + builder);

        // Append Object
        builder = new StringBuilder().append(new Object().getClass());
        System.out.println("Append Object : " + builder);

        // Append chars
        char[] chars = { 'j', 'a', 'v', 'a' };
        builder = new StringBuilder().append(chars);
        System.out.println("Append chars : " + builder);

        // Append charSequence
        builder = new StringBuilder().append("charSequence");
        System.out.println("Append charSequence : " + builder);

        // Append Double
        builder = new StringBuilder().append(10.0d);
        System.out.println("Append Double : " + builder);

        // Append Float
        builder = new StringBuilder().append(10.5f);
        System.out.println("Append Float : " + builder);

        // Append int
        builder = new StringBuilder().append(100);
        System.out.println("Append int : " + builder);

        // Append Boolean
        builder = new StringBuilder().append(true);
        System.out.println("Append Boolean : " + builder);

        // Append Long
        builder = new StringBuilder().append(100000);
        System.out.println("Append Long : " + builder);

        // Append StringBuffer
        builder = new StringBuilder().append(new StringBuffer("stringbuffer"));
        System.out.println("Append StringBuffer : " + builder);

        // Append subarray of the char array
        builder = new StringBuilder().append(chars, 1, 3);
        System.out.println("Append subarray of the char array : " + builder);

        // Append CharSequence
        builder = new StringBuilder().append("javaguides", 0, 9);
        System.out.println("Append CharSequence : " + builder);

        // Append appendCodePoint
        builder = new StringBuilder("javaguides").appendCodePoint(0);
        System.out.println("Append appendCodePoint : " + builder);
    }
}

Output:

Append string : guides
Append char : c
Append Object : class java.lang.Object
Append chars : java
Append charSequence : charSequence
Append Double : 10.0
Append Float : 10.5
Append int : 100
Append Boolean : true
Append Long : 100000
Append StringBuffer : stringbuffer
Append subarray of the char array : ava
Append CharSequence : javaguide
Append appendCodePoint : javaguides

Explanation:

1. The string "guides" is appended.

2. The character 'c' is appended.

3. The string representation of an Object's class is appended.

4. A character array java is appended.

5. The CharSequence "charSequence" is appended.

6. The double value 10.0 is appended.

7. The float value 10.5 is appended.

8. The integer 100 is appended.

9. The boolean true is appended.

10. The long value 100000 is appended.

11. A StringBuffer containing "stringbuffer" is appended.

12. A subarray ava from the character array is appended.

13. A subsequence javaguide from the CharSequence is appended.

14. A codePoint represented as a character is appended, resulting in no visible characters due to the null character.


Comments