StringBuilder 与 StringBuffer - 需要助记符

发布于 2025-01-05 00:01:18 字数 377 浏览 0 评论 0原文

我知道 StringBufferStringBuilder

但如果我半年左右不使用它们,我就会忘记哪个是同步的,哪个是不同步的。我总是查找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡笑忘祈一世凡恋 2025-01-12 00:01:18

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)

如梦初醒的夏天 2025-01-12 00:01:18

我认为新的异步类有更自然的名称。 List,一个定义明确的计算机科学概念,而不是VectorMap,一个定义明确的数学概念,而不是TableBuilder,工厂类的通用名称,而不是Buffer

这对我有用!

New asynchronous classes have more natural names, I think. List, a well defined computer scientific concept, instead of Vector. Map, a well defined mathematical concept, instead of Table. Builder, a common name for a factory class, instead of Buffer.

It works for me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文