5.7 CharSequence as Base Type
So far, you’ve learned how the classes String, StringBuilder, and StringBuffer can store and pass strings. A string is a value object and an important tool in programs since immutable string values are represented by it, while StringBuilder/ StringBuffer comprise mutable strings.
But what happens when a substring is required where it shouldn’t matter whether the original is a String, StringBuffer, or StringBuilder object? And what if read-only access is to be allowed so that changes are excluded? One solution is to expect everything to be a String object (and the Java library does this). But then the program parts that use StringBuilder/ StringBuffer internally must first construct a new string , and that costs ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access