简体   繁体   English

为什么像java中的布尔类包装类是不可变的?

[英]Why Wrapper class like Boolean in java is immutable?

I can't see the reason why the Boolean wrapper classes were made Immutable. 我无法看到布尔包装类成为不可变的原因。

Why the Boolean Wrapper was not implemented like MutableBoolean in Commons lang which actually can be reset. 为什么布尔包装器没有实现像Commons lang中的MutableBoolean实际上可以重置。

Does anyone have any idea/understanding about this ? 有没有人对此有任何想法/理解? Thanks. 谢谢。

Because 2 is 2. It won't be 3 tomorrow. 因为2是2.明天不会是3

Immutable is always preferred as the default, especially in multithreaded situations, and it makes for easier to read and more maintainable code. 永久性首选是永久性的, 特别是在多线程情况下,它使得更容易阅读和更易于维护的代码。 Case in point: the Java Date API, which is riddled with design flaws. 例证:Java Date API,它充满了设计缺陷。 If Date were immutable the API would be very streamlined. 如果Date是不可变的,API将非常简化。 I would know Date operations would create new dates and would never have to look for APIs that modify them. 我知道Date操作会创建新的日期,并且永远不必查找修改它们的API。

Read Concurrency in Practice to understand the true importance of immutable types. 阅读实践中的并发以了解不可变类型的真正重要性。

But also note that if for some reason you want mutable types, use AtomicInteger AtomicBoolean , etc. Why Atomic ? 但是请注意,如果由于某种原因你需要可变类型,请使用AtomicInteger AtomicBoolean等。为什么Atomic Because by introducing mutability you introduced a need for threadsafety. 因为通过引入可变性,您引入了对线程安全性的需求。 Which you wouldn't have needed if your types stayed immutable, so in using mutable types you also must pay the price of thinking about threadsafety and using types from the concurrent package. 如果您的类型保持不变,那么您就不需要这样,因此在使用可变类型时,您还必须考虑使用并发包中的类型来考虑线程安全concurrent Welcome to the wonderful world of concurrent programming. 欢迎来到并发编程的精彩世界。

Also, for Boolean - I challenge you to name a single operation that you might want to perform that cares whether Boolean is mutable. 此外,对于Boolean - 我要求您命名一个您可能想要执行的操作,该操作关注布尔值是否可变。 set to true? 设为真吗? Use myBool = true . 使用myBool = true That is a re-assignment, not a mutation. 这是重新分配,而不是变异。 Negate? 否定? myBool = !myBool . myBool = !myBool Same rule. 同样的规则。 Note that immutability is a feature , not a constraint, so if you can offer it, you should - and in these cases, of course you can. 请注意,不变性是一个特征 ,而不是约束,所以如果你提供它,你应该 - 在这些情况下,你当然可以。

Note this applies to other types as well. 请注意,这也适用于其他类型。 The most subtle thing with integers is count++ , but that is just count = count + 1 , unless you care about getting the value atomically... in which case use the mutable AtomicInteger . 整数最微妙的是count++ ,但这只是count = count + 1 ,除非你关心以原子方式获取值......在这种情况下使用可变的AtomicInteger

Wrapper classes in Java are immutable so the runtime can have only two Boolean objects - one for true, one for false - and every variable is a reference to one of those two. Java中的包装类是不可变的,因此运行时只能有两个布尔对象 - 一个用于true,一个用于false - 每个变量都是对这两个中的一个的引用。 And since they can never be changed, you know they'll never be pulled out from under you. 而且因为它们永远不会改变,你知道它们永远不会从你身下被拉出来。 Not only does this save memory, it makes your code easier to reason about - since the wrapper classes you're passing around you know will never have their value change, they won't suddenly jump to a new value because they're accidentally a reference to the same value elsewhere. 这不仅可以节省内存,而且还可以让您的代码更易于推理 - 因为您知道的包装类永远不会改变它们的价值,它们不会突然跳到新值,因为它们是偶然的在别处引用相同的值。

Similarly, Integer has a cache of all signed byte values - -128 to 127 - so the runtime doesn't have to have extra instances of those common Integer values. 类似地,Integer具有所有带符号字节值的缓存 - -128到127 - 因此运行时不必具有那些常见Integer值的额外实例。

Patashu is the closest. Patashu是最接近的。 Many of the goofy design choices in Java were because of the limitations of how they implemented a VM. Java中许多愚蠢的设计选择都是因为他们实现VM的方式有限。 I think originally they tried to make a VM for C or C++ but it was too hard (impossible?) so made this other, similar language. 我认为最初他们试图为C或C ++制作一个虚拟机,但它太难了(不可能?)所以制作了另一个类似的语言。 Write one, run everywhere! 写一个,到处跑! Any computer sciency justification like those other dudes spout is just after-the-fact folderal. 任何计算机技术证明都像其他人一样狡猾,这只是事后的事情。 As you now know, Java and C# are evolving to be as powerful as C. Sure, they were cleaner. 正如您现在所知,Java和C#正在发展成与C一样强大。当然,它们更清晰。 Ought to be for languages designed decade(s) later! 应该是十年后设计的语言! Simple trick is to make a "holder" class. 简单的诀窍是制作一个“持有者”类。 Or use a closure nowadays! 或者现在使用封闭装置! Maybe Java is evolving into JavaScript. 也许Java正在发展成为JavaScript。 LOL. 大声笑。

Boolean or any other wrapper class is immutable in java. 布尔或任何其他包装类在java中是不可变的。 Since wrapper classes are used as variables for storing simple data, those should be safe and data integrity must be maintained to avoid inconsistent or unwanted results. 由于包装类用作存储简单数据的变量,因此这些应该是安全的,并且必须保持数据完整性以避免不一致或不需要的结果。 Also, immutability saves lots of memory by avoiding duplicate objects. 此外,不变性通过避免重复对象来节省大量内存。 More can be found in article Why Strings & Wrapper classes are designed immutable in java? 更多内容可以在文章中找到为什么Strings&Wrapper类在java中设计为不可变的?

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

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