StringBuilder and code points
What you will learn
StringBuilder holds editable text, useful when assembling many pieces. For Unicode text, distinguish UTF-16 code units from code points: a supplementary code point occupies two units, and a visible character may comprise several code points.
public class Main { public static void main(String[] args) { int[] counts = {2, 5, 9}; StringBuilder out = new StringBuilder(); for (int i = 0; i < counts.length; i++) { if (i > 0) out.append(", "); out.append(counts[i]); } String snapshot = out.toString(); out.append(" done"); System.out.println(snapshot); System.out.println(out); }}Expected output:
2, 5, 92, 5, 9 doneThe immutable snapshot stays unchanged.
How it works
new StringBuilder() starts empty; append adds content to the same builder and returns it, enabling chaining. toString() produces a String snapshot. Keep builders local to a single thread unless access is properly coordinated. For short concatenations, + is often clearer; no fixed performance claim is made. String.length() counts UTF-16 units; codePointCount(0, text.length()) counts code points, counting isolated surrogates individually. offsetByCodePoints translates a code point offset into a UTF-16 index for substring. A user-perceived character (grapheme cluster) can contain multiple code points. Code point counting is not full text segmentation. toLowerCase(Locale.ROOT) provides locale-independent case conversion for appropriate machine identifiers; case conversion need not preserve length.
public class Main { public static void main(String[] args) { String text = "A\uD83D\uDE80"; String combined = "e\u0301"; System.out.println(text.length()); System.out.println(text.codePointCount(0, text.length())); System.out.println(combined.codePointCount(0, combined.length())); }}Expected output:
322The rocket is supplementary; e plus combining acute is two code points despite often looking like one character.
Check a boundary or unusual input
import java.util.Locale;public class Main { public static void main(String[] args) { String text = "\uD83D\uDE80A"; int end = text.offsetByCodePoints(0, 1); String first = text.substring(0, end); System.out.println(first.codePointAt(0)); System.out.println(first.length()); System.out.println("ITEM".toLowerCase(Locale.ROOT)); }}Expected output:
1286402itemU+1F680 is preserved as two UTF-16 units. Locale.ROOT gives stable identifier case.
Common mistakes
Never assume charAt returns a complete supplementary character or split arbitrary text at an unchecked code-unit position. A StringBuilder is mutable and equals does not compare contents. Compare snapshots as Strings.
Try it yourself
Write static String joinCounts(int[] counts) for non-null int arrays of at most 100 elements, with comma separators and no trailing separator. Empty input returns empty text.
Save each example independently as Main.java; compile with javac --release 21 -encoding UTF-8 Main.java and launch using java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
StringBuilder and code points — knowledge check
Practice core behavior and boundary cases.
What does builder.append("x") change?
How many UTF-16 units represent U+1F680?
What does codePointCount measure?
What happens to a snapshot after builder editing?
Which converts a code point offset to a UTF-16 index?