Building Strings with StringBuilder
Efficient concatenation.
Building Strings with StringBuilder is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Mutable String Builder
StringBuilder is a mutable sequence of characters. Unlike String, you can change its contents in place, which makes building text efficient.
Creating a StringBuilder
Create one with new StringBuilder() for an empty builder, or pass initial text to start with content.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
StringBuilder named = new StringBuilder("Hello");
System.out.println("empty length: " + sb.length());
System.out.println("named: " + named);
}
}Appending Text
The append method adds content to the end. It accepts strings, numbers, chars, booleans, and more.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("Count: ");
sb.append(5);
sb.append(' ');
sb.append(true);
System.out.println(sb);
}
}Method Chaining
append returns the same builder, so calls can be chained fluently in one expression.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("a").append("b").append("c").append(123);
System.out.println(sb);
}
}Building in a Loop
This is the main use case. Append inside a loop without creating a new object each time, then convert once at the end.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10; i++) {
sb.append(i).append(' ');
}
System.out.println(sb.toString().trim());
}
}Converting to a String
Call toString() to get an ordinary immutable String out of the builder once you are done modifying it.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("result");
String finalText = sb.toString();
System.out.println(finalText.toUpperCase());
}
}Joining With a Separator
You can build comma-separated lists by appending a separator between items, handling the last one carefully.
public class Main {
public static void main(String[] args) {
String[] names = {"Ann", "Ben", "Cara"};
StringBuilder sb = new StringBuilder();
for (int i = 0; i < names.length; i++) {
sb.append(names[i]);
if (i < names.length - 1) {
sb.append(", ");
}
}
System.out.println(sb);
}
}Reading the Length
length() tells you how many characters the builder currently holds. It updates as you append.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("12345");
System.out.println("Length: " + sb.length());
}
}Accessing Characters
Use charAt(index) to read a single character by position, just like with a String.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Coddy");
System.out.println("First: " + sb.charAt(0));
System.out.println("Last: " + sb.charAt(sb.length() - 1));
}
}Capacity vs Length
A builder keeps an internal buffer (capacity) that grows automatically. You can pass an initial capacity to reduce resizing when you know the final size.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(100);
sb.append("short");
System.out.println("Length: " + sb.length());
System.out.println("Capacity at least: " + (sb.capacity() >= 100));
}
}A Practical Builder
Putting it together: build a small report string efficiently with chained appends and a loop.
public class Main {
public static void main(String[] args) {
int[] sales = {100, 250, 75};
StringBuilder report = new StringBuilder("Sales Report\n");
int total = 0;
for (int i = 0; i < sales.length; i++) {
report.append("Day ").append(i + 1).append(": ").append(sales[i]).append('\n');
total += sales[i];
}
report.append("Total: ").append(total);
System.out.println(report);
}
}Quick Check
Why is StringBuilder more efficient than repeated + concatenation in a loop?
Recap
You learned to build strings with StringBuilder:
- It is a mutable character sequence
appendadds content and supports chaining- Ideal for building text in loops
- Call
toString()to get the finalString - An initial capacity avoids resizing
Frequently asked questions
Is the “Building Strings with StringBuilder” lesson free?
Yes — the full text of “Building Strings with StringBuilder” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building Strings with StringBuilder”?
Efficient concatenation. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building Strings with StringBuilder” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Strings Are Immutable
- Building Strings with StringBuilder
- StringBuilder Methods
- StringBuilder vs StringBuffer