I have read in a Java book that says:
Because a
Stringis immutable, usingStringBufferis more efficient.
I understand that String instances are immutable.
I also understand that StringBuffer makes processing Strings more effcient than normal.
But the thing I can’t work out is what connects these two concepts, i.e. how does String being immutable help StringBuffer?
Thanks 🙂
It doesn’t. I think that you are simply misinterpreting the sentence that you quoted.
It is saying that StringBuffer is a relative more efficient option (for certain tasks). In other words: “Because a String is immutable, using StringBuffer is more efficient [than using String for certain tasks].”.
It is NOT saying that
StringBufferis faster in absolute terms than it would be if String wasn’t immutable. Certainly, that’s not how I read the quote … and it is not a true statement either.