StringBuffer is obsolete?

后端 未结 5 1552
梦毁少年i
梦毁少年i 2021-02-03 17:16

In the book \"Effective Java\", Josh Bloch says that

StringBuffer is largely obsolete and should be replaced by the non-synchronized implementation \'S

5条回答
  •  耶瑟儿~
    2021-02-03 18:09

    I think obsolete is an overstatement.

    StringBuffer is synchronized. StringBuilder is not.

    In many (maybe most) cases, you won't care about the thread safety of something used to build strings. You should use StringBuilder in these cases. In some cases, however, you may very well want to make sure actions on the object is thread safe. StringBuffer is still useful in those cases.

提交回复
热议问题