String, StringBuffer, and StringBuilder

后端 未结 11 1560
谎友^
谎友^ 2020-11-22 13:52

Please tell me a real time situation to compare String, StringBuffer, and StringBuilder?

11条回答
  •  感动是毒
    2020-11-22 14:28

    Note that if you are using Java 5 or newer, you should use StringBuilder instead of StringBuffer. From the API documentation:

    As of release JDK 5, this class 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.

    In practice, you will almost never use this from multiple threads at the same time, so the synchronization that StringBuffer does is almost always unnecessary overhead.

提交回复
热议问题