2

There are many differences between String and (StringBuilder or StringBuffer) like mutability and many string operations

May be this question seems a bit silly, but I want to know for the sake of programming paradigm.

I want to ask, why has Java implemented another class, StringBuilder or StringBuffer for a data structure like String. Why have they not given those features in String itself.

Why not make String itself thread-safe or provide some extra features that StringBuilder or StringBuffer has?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Aman Singh
  • 743
  • 1
  • 10
  • 20
  • 1
    http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer – bobs_007 Jun 04 '15 at 06:00
  • @bobs_007 - I know the difference between StringBuffer and StringBuilder, Please read my question i.e. bold in my post – Aman Singh Jun 04 '15 at 06:02
  • If I could remember well String is Thread Safe! – Lrrr Jun 04 '15 at 06:02
  • 1
    `String` is immutable, `StringBuilder` is mutable. It's as simple as that. You can't add the `StringBuilder` features to `String`, because they're all about mutability. – Jon Skeet Jun 04 '15 at 06:05
  • @Lrrr It's mutable, that is the purpose of introducing StringBuilder and StringBuffer. String is immutable but if you want to perform String based operations you have two choices, if you want it thread safe way without creating new instances of string you use StringBuffer – Bilbo Baggins Jun 04 '15 at 06:06
  • @Lrrr: Whoops, typo - fixed, thanks. – Jon Skeet Jun 04 '15 at 06:06
  • 1
    @JonSkeet Its honor to even fix Jon Skeet's typos :) – Lrrr Jun 04 '15 at 06:07
  • Possible duplicate: http://stackoverflow.com/questions/93091/why-cant-strings-be-mutable-in-java-and-net – assylias Jun 04 '15 at 06:10

1 Answers1

7
  1. String is immutable and there are many reason and also benefit to it. Why? and what the necessity of it? (very popular topic ) search or read those why-is-string-immutable-in-java or why-string-is-immutable-in-java

  2. Now Some one need to do frequent String operation here comes StringBuffer which is thread safe (synchronized).

  3. Some one don't need thread safety here comes StringBuilder.

Now some one can still use StringBuffer when thread safety is not necessary, but that would be slow. That's why both of them is important.

String's are differently handled by jvm If those features of StringBuffer was added it will not be immutable any more.


Update : point 2 and 3 are altered from the comment of @Jon Skeet.

Community
  • 1
  • 1
Saif
  • 6,804
  • 8
  • 40
  • 61
  • What types of reasons are you trying to say about in your first point ? – Aman Singh Jun 04 '15 at 06:07
  • 7
    Actually `StringBuffer` came before `StringBuilder`, and then Sun realized that it's incredibly rare to need its thread-safety, so they introduced `StringBuilder`. If `StringBuilder` had come first, I doubt that `StringBuffer` would ever have been introduced. – Jon Skeet Jun 04 '15 at 06:07
  • thanks Mr. @JonSkeet for making that clear. – Saif Jun 04 '15 at 06:22
  • reading those link will give you some idea and as I said its very popular topic so you can easily search . – Saif Jun 04 '15 at 06:25