java StringBuffer 与 StringBuilder 与 StringTokenizer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3785862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:21:59  来源:igfitidea点击:

StringBuffer vs StringBuilder Vs StringTokenizer

java

提问by Dead Programmer

what is the difference between the StringBuffer vs StringBuilder Vs StringTokenizer on the internal implementation. when to use these . kindly waiting for the answer.

StringBuffer 与 StringBuilder 与 StringTokenizer 在内部实现上的区别是什么。何时使用这些。敬请期待答案。

Update:-

更新:-

I am also going through the source code.

我也在浏览源代码。

回答by Péter T?r?k

StringBuffer- introduced in JDK 1.0 - is thread safe (all of its methods are synchronized), while StringBuilder- since JDK 1.5 - is not. Thus it is recommended to use the latter under normal circumstances.

StringBuffer- 在 JDK 1.0 中引入 - 是线程安全的(它的所有方法都是synchronized),而StringBuilder- 自 JDK 1.5 - 不是。因此建议在正常情况下使用后者。

StringTokenizeris meant for a whole different purpose then the former two: cutting strings into pieces, rather than assembling. As @Henning noted, it is also "retired" since JDK 1.5 - it is recommended to use String.splitinstead.

StringTokenizer与前两者的目的完全不同:将字符串切成碎片,而不是组装。正如@Henning 所指出的,它也从 JDK 1.5 开始“退休”——建议改用它String.split

回答by Jigar Joshi

  • StringBuffer is designed to be thread-safe and all public methods in StringBuffer are synchronized. StringBuilder does not handle thread-safety issue and none of its methods is synchronized.

  • StringBuilder has better performance than StringBuffer under most circumstances.

  • Use the new StringBuilder wherever possible.

  • StringBuffer 被设计为线程安全的并且 StringBuffer 中的所有公共方法都是同步的。StringBuilder 不处理线程安全问题,并且它的任何方法都不同步。

  • 在大多数情况下,StringBuilder 比 StringBuffer 具有更好的性能。

  • 尽可能使用新的 StringBuilder。

Hereis performance comparisonof StringBuilder & StringBuffer

这里performance comparisonStringBuilder的&StringBuffer的

StringBuilder & StringBuffer Holds String where StringoTokeizer class allows an application to break a string into tokens .. So It is like odd one out

StringBuilder 和 StringBuffer 保存字符串,其中 StringoTokeizer 类允许应用程序将字符串分解为标记 .. 所以它就像一个奇怪的

回答by Gadolin

StringBuffer - is synchronized version of StringBuilder (introduced after its unsynchronized peer)

StringBuffer - 是 StringBuilder 的同步版本(在其未同步的对等体之后引入)

回答by keshav84

StringBuffer serves same purpose as StringBuilder except that StringBuffer is thread-safe.
StringTokenizer is used for splitting the string into tokens based on some delimiters.

除了 StringBuffer 是线程安全的外,StringBuffer 的用途与 StringBuilder 相同。
StringTokenizer 用于根据一些分隔符将字符串拆分为标记。