简体   繁体   English

不可变或不可更改集合的目的

[英]Purpose of Immutable or Unmodifiable Collections

What are the real benefits of Immutable or Unmodifiable collections, or objects? 不可变或不可修改的集合或对象的真正好处是什么?

As programmers, we try to write code without bugs, such that a user cannot break the system, and that bad actors cannot hack into it. 作为程序员,我们尝试编写没有错误的代码,以使用户无法破坏系统,并且不良行为者也无法侵入系统。 To me, Immutable objects seem to try to prevent programmers from making silly mistakes. 在我看来,不可变对象似乎试图防止程序员犯傻错误。

Is there more to it than that? 还有什么呢?

不可变的类和集合确实有助于避免尝试修改同一对象的多个线程的并发问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM