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.
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
Parameter
Description
str / obj / primitive
The text, object, CharSequence, char[], or primitive value to convert and add at the end.
start, end / offset, len
Optional slice bounds for the CharSequence or char[] overloads.
Return Value
Return Value
Description
StringBuilder
A reference to this builder, allowing method chaining.
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());
}
}
Output
Order #42 | total=$19.5 | paid=true!
length = 36
How It Works
Start with a prefix.new StringBuilder("Order #") seeds the mutable buffer with seven characters.
Chain in one statement. Each append returns this, converting 42, 19.5, true, and '!' via String.valueOf semantics.
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());
}
}
Output
java, loops, strings
How It Works
One shared buffer. Creating csv outside the loop avoids allocating new String objects on each iteration.
Guard the comma. Appending ", " only when i > 0 prevents a leading or trailing separator.
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 nullString, and see that a nullchar[] throws.
Array slice.sb.append(letters, 1, 3) reads three characters starting at index 1, adding BCD.
Null String becomes text. Passing a typed nullString variable appends the four letters "null".
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.