为什么String类要这样设计呢?

发布于 2024-12-04 16:58:18 字数 80 浏览 1 评论 0原文

为什么 String 类的设计方式是该类的实例被池化且不可变?

谢谢&问候, 维迪亚卡·夏尔马。

Why was the String class designed in a way that instances of this class are pooled as well as immutable?

Thanks & Regards,
Vidyakar Sharma.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

一片旧的回忆 2024-12-11 16:58:18

字符串对象通常不会被池化 - 只有字符串常量会通过实习自动池化。 (当然,您可以手动调用intern,甚至可以通过HashSet等创建您自己的池。)这只是安全的因为字符串是不可变的 - 确保任何编译时常量仅在内存中出现一次是有意义的。

您不想为系统中的每个字符串在实习池中查找字符串(或永远保留它)付出代价,因为随着时间的推移可能会有许多不同的字符串。然而,只要这些类存在,从类加载的字符串常量就会一直存在,并且通过将它们驻留一次,您可以减少 GC 流失所需的内存。

String objects aren't usually pooled - only string constants are pooled automatically via interning. (You can call intern manually of course, or even create your own pools via HashSet<String> etc.) This is only safe because strings are immutable - and it makes sense to make sure that any compile-time constant only occurs once in memory.

You wouldn't want to pay the price of looking up the string in the intern pool (or keeping it around forever) for every string in the system, because there may be many different strings over time. However, the string constants loaded from classes will stick around as long as those classes do, and by interning them once you can reduce the memory required as GC churn.

梦纸 2024-12-11 16:58:18

如果 String 不是不可变的,那么您将无法

  • 在不破坏封装的情况下安全地从 getter 返回 String 字段,因为调用者可能会在您背后修改字符串的内容
  • 以在线程之间共享 String,因为某些线程可能会修改它的内容。对字符串的所有访问都必须同步。
  • 使用字符串作为 HashMaps/TreeMaps 中的键,因为有人可能会更改其值,从而更改其 hashCode/比较顺序以
  • 池化字符串,以便使同一常量字符串的单个实例具有
  • 共享其字符串的相同 char 数组的子字符串

简而言之,生活会变得更加复杂,因为你必须在各处制作 String 的防御性副本,并且 StackOverflow 会充斥着有关微妙错误的问题,其中某些 String 存储在映射中但无法再找到。

If String weren't immutable you wouldn't be able

  • to safely return a String field from a getter without breaking encapsulation, because the caller might modify the contents of the string behind your back
  • to share a String between threads, because some thread might modify its content. All the accesses to the String would have to be synchronized.
  • to use Strings as keys in HashMaps/TreeMaps, because someone might change its value and thus its hashCode/comparison order
  • to pool Strings in order to have a single instance of the same constant string
  • to have a substring sharing the same char array of its string

In short, life would be much more complicated, because you would have to make defensive copies of the String everywhere, and StackOverflow would be flooded with questions regarding subtle bugs where some String is stored in a map but can't be found anymore.

尽揽少女心 2024-12-11 16:58:18

不可变对象是现有的最佳设计决策之一。它的目的是简化并发编程。共享该对象的线程不能互相干扰。

如果您想要可变字符串,请查看:StringBuffer 和 StringBuilder

An Immutable object is one of the best design decisions that exists. It's intended to simplify concurrent programming. Threads sharing the object can not interfere with each other.

If you want mutable strings check out: StringBuffer and StringBuilder

长途伴 2024-12-11 16:58:18

对于可变字符串,请查看:java.lang 中的 StringBuffer 和 StringBuilder。

for mutable strings check out: StringBuffer and StringBuilder from java.lang.

留蓝 2024-12-11 16:58:18

合并以避免出现表示相同对象的重复对象。

不可变,使共享更容易。

Pooled to avoid having duplicate objects that represent the same.

Immutable to make it easier to share it.

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