Dante May Code
Dante May Code

Reputation: 11247

Why do String and StringBuilder/StringBuffer have different method sets?

Because String and StringBuilder (or StringBuffer) are designed for different purposes, I can understand the reasons for half of the difference (such as String does not have append and delete). However, there are a few points still confusing me.

To name a few:

I know they are by design, but why?

Upvotes: 0

Views: 1866

Answers (2)

Meisam
Meisam

Reputation: 435

As of release JDK 5, StringBuffer has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

Upvotes: -1

Peter Lawrey
Peter Lawrey

Reputation: 533560

String and StringBuilder were designed and implements more than a decade apart. What was a good idea in Java 1.0 wasn't considered a good idea in Java 5.0.

e.g. trim() trims characters <= space. This include graphical characters, but not all whitespace e.g. (char) 160 is a whitespace, but not trimmed.

Upvotes: 2

Related Questions