Java StringBuilder append() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.StringBuilder

Definition

In Java, sb.append(value) adds text or a value's text form onto the end of a StringBuilder in place instead of creating a new String on every edit.

Every overload returns this, letting you chain calls like sb.append("Item #").append(1).append(": ").append(9.5).

An instance method of the java.lang.StringBuilder class (since Java 1.5, JDK 1.5): a final, mutable sequence of UTF-16 characters that is not thread-safe. Call it on an existing builder, as in sb.append(...). No import is needed because java.lang is always visible.

Why loops use append()

Repeating += on a String in a loop copies prior text each pass; sb.append(...) writes into one resizable buffer.

Syntax

Call append on a StringBuilder with any value, a CharSequence range, or a char[] slice.

Java
StringBuilder sb = new StringBuilder("User: ");
sb.append("Ada").append(" #").append(7).append(true);
sb.append(new char[]{'A', 'B', 'C'}, 0, 2); // appends "AB"
CallWhat it does
sb.append("text")Appends a String (or "null" if null).
sb.append(42)Converts an int, long, float, double, or boolean to text.
sb.append(chars, offset, len)Appends len chars from char[] starting at offset.
sb.append(seq, start, end)Appends [start, end) from a CharSequence.

For supplementary code points passed as int, use appendCodePoint() rather than append(int).

Library declarations
Java
// java.lang.StringBuilder
public StringBuilder append(Object obj)
public StringBuilder append(String str)
public StringBuilder append(StringBuffer sb)
public StringBuilder append(CharSequence s)
public StringBuilder append(CharSequence s, int start, int end)
public StringBuilder append(char[] str)
public StringBuilder append(char[] str, int offset, int len)
public StringBuilder append(boolean b)
public StringBuilder append(char c)
public StringBuilder append(int i)
public StringBuilder append(long lng)
public StringBuilder append(float f)
public StringBuilder append(double d)

Parameters

ParameterDescription
str / obj / primitiveThe text, object, CharSequence, char[], or primitive value to convert and add at the end.
start, end / offset, lenOptional slice bounds for the CharSequence or char[] overloads.

Return Value

Return ValueDescription
StringBuilderA reference to this builder, allowing method chaining.

Compare with Similar Methods

Ways to add text in Java:

MethodHow it differs
insert(offset, x)Adds text at any index from 0 to length() instead of only at the end.
appendCodePoint(cp)Appends a Unicode character for an int code point rather than decimal digits.
String.concat(str)Returns a new immutable String; only accepts String and throws on null.
toString()Turns the finished builder into an immutable String.

Java Examples

Example 1 — Chain Mixed Types Into a Receipt Line

Build one formatted summary line by chaining String, int, double, boolean, and char appends.

Java
public class StringBuilderAppendChain {
    public static void main(String[] args) {
        StringBuilder line = new StringBuilder("Order #");
        line.append(42)
            .append(" | total=$")
            .append(19.5)
            .append(" | paid=")
            .append(true)
            .append('!');

        System.out.println(line.toString());
        System.out.println("length = " + line.length());
    }
}

How It Works

  1. Start with a prefix. new StringBuilder("Order #") seeds the mutable buffer with seven characters.
  2. Chain in one statement. Each append returns this, converting 42, 19.5, true, and '!' via String.valueOf semantics.
  3. Convert when finished. Calling toString() produces the final text of length 36 without modifying the builder.

Example 2 — Build a Comma-Separated List in a Loop

Collect array items into one StringBuilder, adding separators only between elements.

Java
public class StringBuilderAppendCsv {
    public static void main(String[] args) {
        String[] tags = {"java", "loops", "strings"};
        StringBuilder csv = new StringBuilder();

        for (int i = 0; i < tags.length; i++) {
            if (i > 0) {
                csv.append(", ");
            }
            csv.append(tags[i]);
        }

        System.out.println(csv.toString());
    }
}

How It Works

  1. One shared buffer. Creating csv outside the loop avoids allocating new String objects on each iteration.
  2. Guard the comma. Appending ", " only when i > 0 prevents a leading or trailing separator.
  3. Clean output. The three words join smoothly into java, loops, strings.

Example 3 — Append a char[] Slice and Handle null Arguments

Copy part of a char[], append a null String, and see that a null char[] throws.

Java
public class StringBuilderAppendSliceNull {
    public static void main(String[] args) {
        char[] letters = {'A', 'B', 'C', 'D', 'E'};
        String missing = null;
        char[] noArray = null;

        StringBuilder sb = new StringBuilder("Code: ");
        sb.append(letters, 1, 3);
        sb.append('-');
        sb.append(missing);
        System.out.println(sb.toString());

        try {
            sb.append(noArray);
        } catch (NullPointerException e) {
            System.out.println("char[] null threw NullPointerException");
        }
    }
}

How It Works

  1. Array slice. sb.append(letters, 1, 3) reads three characters starting at index 1, adding BCD.
  2. Null String becomes text. Passing a typed null String variable appends the four letters "null".
  3. Null char[] throws. The char[] overload tries to read the array length and raises NullPointerException.

Frequently Asked Questions

It returns a reference to the same StringBuilder object (this), so you can chain multiple append calls in a single expression.
A null String, Object, StringBuffer, or CharSequence appends the text "null". A null char[] throws NullPointerException, and an uncast append(null) does not compile because the overload is ambiguous.
Strings are immutable, so repeating s += part in a loop copies the accumulated text repeatedly. StringBuilder.append() writes into one mutable, resizable buffer.
Yes. Overloads exist for boolean, char, int, long, float, and double, formatting each one just like String.valueOf().
Use the three-argument overload sb.append(charArray, offset, len) to specify the starting index and how many characters to copy.

Next: appendCodePoint()

Append a full Unicode code point (1 or 2 UTF-16 chars) onto the end of the builder.

appendCodePoint() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful