Reputation: 11247
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:
getBytes
and getChars
but StringBuilder has only getChars
.replace
of different functionality respectively.toLowerCase
and toUpperCase
, whereas StringBuilder does not.trim
, whereas StringBuilder does not.I know they are by design, but why?
Upvotes: 0
Views: 1866
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
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