Why StringBuffer has a toStringCache while StringBuilder not?

Viewed 825

In JDK 8, StringBuffer class has a toStringCache, while StringBuilder doesn't.

/**
 * A cache of the last value returned by toString. Cleared
 * whenever the StringBuffer is modified.
 */
private transient char[] toStringCache;

But why?

  • One possible reason I can think of is that StringBuffer is already synchronized so a cache can be implemented easier.

  • Or maybe historically StringBuffer was implemented this way so old code depends heavily on this feature?

Given modern JVM with escape analysis and biased locking, is the difference relevant anymore?

2 Answers
Related