Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 201 ยท Strings

StringBuffer, StringJoiner and StringTokenizer

Compare StringBuffer and StringBuilder, join text with StringJoiner, String.join and Collectors.joining, and replace legacy StringTokenizer with split.

Java has a few more classes for building and taking apart text. StringBuffer is the older, thread-safe sibling of StringBuilder. StringJoiner, String.join and Collectors.joining glue many values together with separators, which is one of the most common jobs in real code. And StringTokenizer is a legacy class for splitting text that you'll still find in older projects and competitive-programming code.

By the end of this lesson you'll know which of these to pick, which to avoid, and the surprising rules of split that the old tokenizer handled differently.

StringBuffer: the synchronized builder

StringBuffer has been in Java since version 1.0. Its API is practically identical to StringBuilder: append, insert, delete, replace, reverse, setLength, capacity and the rest all behave the same way.

The difference is that every public StringBuffer method is synchronized. Only one thread at a time can run a method on a given buffer, so two threads appending at once can't corrupt it. StringBuilder was added in Java 5 as the same class without the locking, because almost all builders are used by a single thread, and the locking was pure overhead.

Java Same API, different class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

What thread safety buys you

Here four threads append 10,000 characters each to a shared StringBuilder and a shared StringBuffer. Threads are covered properly in Threads; for now just notice the final lengths:

Java Sharing a builder between threads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The StringBuffer always ends with exactly 40,000 characters. The StringBuilder result changes from run to run: usually fewer characters, because threads overwrote each other's writes, and sometimes an internal ArrayIndexOutOfBoundsException (caught here). Run it a few times. (Thread.ofPlatform().start(work) is a Java 21 builder call that creates and starts a thread.)

Why you still shouldn't use StringBuffer

Thread safety on each method sounds great, but it rarely solves real problems:

  • Builders are almost always local variables. A builder created inside a method is only seen by the thread running that method, so there is nothing to protect.
  • Single methods are atomic; sequences are not. If two threads each run buffer.append(name).append(": ").append(score), each append is safe, but the three calls of one thread can still interleave with the other thread's calls, producing garbled lines. You'd need your own lock around the whole sequence anyway.
  • Concurrent text building has better designs. Let each thread build its own string with a StringBuilder, then combine the finished pieces, for example with a concurrent collection.

Modern JVMs make uncontended locks cheap, so StringBuffer isn't dramatically slower, but there is no reason to pay for locking you don't need.

Rule of thumb: use StringBuilder everywhere. You'll meet StringBuffer in old code and in a few older APIs (for example, Matcher.appendReplacement originally accepted only a StringBuffer; Java 9 added StringBuilder overloads). Replacing it with StringBuilder in single-threaded code is a safe, common clean-up.

StringBuilder StringBuffer
Mutable Yes Yes
Thread-safe methods No Yes (synchronized)
Since Java 5 Java 1.0
Speed in one thread Faster Slightly slower
Use it for Practically everything Legacy APIs and old code

Joining values with StringJoiner

Joining a list with separators is surprisingly fiddly by hand: you must avoid a stray comma before the first item or after the last. java.util.StringJoiner handles it for you. Give it a delimiter, and optionally a prefix and suffix:

Java StringJoiner basics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Notes on the behavior:

  • With no elements, the result is just prefix + suffix, unless you called setEmptyValue.
  • setEmptyValue only applies while nothing has been added. Adding an empty string counts as an element, so the empty value is no longer used.
  • add accepts any CharSequence; add(null) adds the text "null".
  • merge(otherJoiner) appends another joiner's content (without its prefix and suffix) as a single element.

String.join: the one-liner

When you don't need a prefix or suffix, String.join is shorter. It takes a delimiter and either varargs or any Iterable of strings:

Java
String.join(", ", "Oslo", "Lima", "Pune");      // "Oslo, Lima, Pune"
String.join(" | ", List.of("home", "about"));   // "home | about"
String.join("-", List.of());                    // ""

String.join uses a StringJoiner internally, so it behaves the same way.

Collectors.joining with streams

With streams, Collectors.joining does the same job at the end of a pipeline. It has three forms: no arguments, a delimiter, or a delimiter plus prefix and suffix. Because a stream can transform elements first, it also works for non-string values:

Java Collectors.joining

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Collectors.joining only accepts CharSequence elements, which is why each pipeline maps its values to strings first. You'll learn the whole collectors family in Collecting Stream Results.

Need Use
Join an existing list or a few values String.join
Join with a prefix/suffix, or build up in a loop StringJoiner
Join at the end of a stream Collectors.joining
Join with complex formatting per element StringBuilder

StringTokenizer: legacy splitting

java.util.StringTokenizer breaks a string into tokens separated by delimiter characters. Its documentation has long called it a legacy class kept for compatibility and recommended String.split instead. You should know how to read it:

Java StringTokenizer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Key traits:

  • The default delimiters are whitespace: space, tab, newline, carriage return and form feed.
  • The delimiter string is a set of single characters. new StringTokenizer(text, "ab") splits on a or b, not on the word "ab".
  • Consecutive delimiters never produce empty tokens. That is handy for messy whitespace and wrong for CSV, where an empty field matters.
  • It doesn't understand regular expressions, quotes or escapes.

The modern replacement: split

String.split takes a regular expression (see Regular Expressions) and returns an array. It's more powerful, but its edge cases differ from StringTokenizer, and they cause real bugs:

Java split edge cases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

What the output shows:

  1. Inner empty fields are kept, but trailing empty strings are removed. Pass a negative limit such as -1 to keep them all.
  2. A positive limit caps the number of parts; the last part holds the rest of the string.
  3. Leading delimiters produce a leading empty string, so strip() before splitting on whitespace.
  4. Characters like ., |, *, +, ?, $ and \ are regex metacharacters. "v1.2.3".split(".") returns an empty array because every character is a delimiter and all the resulting empty strings are trailing. Escape the character ("\\.") or use Pattern.quote.

For huge inputs or when you want a stream, Pattern.compile(",").splitAsStream(text) splits lazily, and a Scanner can read tokens of different types (see Reading User Input).

StringTokenizer String.split
Delimiter Set of characters Regular expression
Empty tokens Always skipped Kept (except trailing, by default)
Result Iterate with hasMoreTokens/nextToken String[]
Status Legacy Recommended

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main difference between StringBuffer and StringBuilder?

    Both are mutable with nearly identical APIs. StringBuffer locks on every method call; StringBuilder skips the locking, which is why it is preferred in single-threaded code.
  2. 2. What does this print? var j = new StringJoiner("-", "<", ">"); j.setEmptyValue("EMPTY"); IO.println(j);

    No element was added, so the joiner uses the empty value set with setEmptyValue. Without it, the result would be "<>".
  3. 3. How many elements does "a,,b,,".split(",") return?

    The result is ["a", "", "b"]. The inner empty field is kept, but trailing empty strings are removed. With split(",", -1) you'd get 5 elements.
  4. 4. What does new StringTokenizer("a--b", "--").countTokens() return?

    The delimiter argument is a set of characters, here just '-'. Consecutive delimiters never produce empty tokens, so the tokens are "a" and "b".
  5. 5. Why does "1.5.2".split(".") return an empty array?

    In a regular expression, . matches any character. Escape it as "\." or use Pattern.quote(".").

Summary

  • StringBuffer is a synchronized StringBuilder from Java 1.0. Prefer StringBuilder; per-method locking rarely makes real code thread-safe.
  • StringJoiner joins values with a delimiter and optional prefix/suffix; setEmptyValue controls the result when nothing was added.
  • String.join is the quick one-liner for lists and varargs; Collectors.joining ends a stream pipeline.
  • StringTokenizer is legacy: it splits on a set of characters and skips empty tokens.
  • String.split uses a regex, keeps inner empty fields, drops trailing ones unless the limit is negative, and needs metacharacters like . and | escaped.

Next: Comparing and Converting Strings, where you'll compare text correctly (including language-aware sorting) and convert between strings, numbers and arrays.