Building Strings with StringBuilder
Efficient concatenation.
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);
}
}All lessons in this course
- Why Strings Are Immutable
- Building Strings with StringBuilder
- StringBuilder Methods
- StringBuilder vs StringBuffer