StringBuilder 与 StringBuffer - 需要助记符
我知道 StringBuffer
和 StringBuilder
。
但如果我半年左右不使用它们,我就会忘记哪个是同步的,哪个是不同步的。我总是查找 JavaDoc 的第一句话。
那么:有没有什么好记的助记符来区分它们呢? 您如何记住其中的区别?
I know the technical differences between StringBuffer
and StringBuilder
.
But if I don't use them for half a year or so, I just forget which one is synchronized and which one is not. I always have lookup the first sentence of the JavaDoc.
So: Is there some kind of easy to remember mnemonic to distinguish them? How do YOU remember the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1
StringBuffer 是较旧的实现。旧的集合实现也被同步。
现在如何记住缓冲区比构建器旧?想一想,如何调用能够包含可转换为字符串的字符缓冲区的类?答案是
StringBuffer
。这是 Sun Microsystem 的人最初开发此类时的想法。然后他们想为几乎相同的东西找到另一个名称,并记住它实际上实现了 Builder 模式。后来模式变得更加流行,所以他们将新的类称为Builder。
我希望这有帮助。
2
其他助记符。比较作品:
缓冲
。
区别在于第三个字母,即单词“first”的第一个字母 这意味着缓冲区是第一个
3
只需按字母顺序对这两个词进行排序即可:Buffer、Builder。缓冲区是第一,构建器是第二。因此缓冲区是同步的(参见我的故事的开头)
1
StringBuffer is older implementation. Older implementations of collections were also synchronized.
Now how to remember that buffer is older than builder? Think, how would you call class that is able to contain buffer of characters that can be transformed to string? The answer is
StringBuffer
. This is what the guys from Sun Microsystem thought when they initially developed this class.Then they wanted to find yet another name for almost the same thing and remembered that it actually implements
Builder
pattern. Pattern became more fashionable later, so they called the new class Builder.I hope this helps.
2
Other mnemonics. Compare the workds:
Buffer
Builder
The difference is in the3 3rd letter that is the first letter of word "first". It means that buffer was first
3
Just sort these 2 words alphabetically: Buffer, Builder. Buffer is first, builder is second. Therefore buffer is synchronized (see the beginning of my story)
我认为新的异步类有更自然的名称。
List
,一个定义明确的计算机科学概念,而不是Vector
。Map
,一个定义明确的数学概念,而不是Table
。Builder
,工厂类的通用名称,而不是Buffer
。这对我有用!
New asynchronous classes have more natural names, I think.
List
, a well defined computer scientific concept, instead ofVector
.Map
, a well defined mathematical concept, instead ofTable
.Builder
, a common name for a factory class, instead ofBuffer
.It works for me!