为什么Java中的BigInteger设计为不可变的? [英] Why BigInteger in java is designed to be immutable?

查看:237
本文介绍了为什么Java中的BigInteger设计为不可变的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,BigInteger是不可变的,但是我想了解为什么,因为很多时候它被用于执行很多可以产生很多对象的计算.因此,将其设置为不变是一种直观的感觉.我想到的情况类似于字符串操作,然后是StringBuilder的选项.是否应该有BigInteger的不可变对象?我认为这在很多情况下可能是有益的.

In java , BigInteger is immutable but I want to understand why because a lot of times it is used to do a lot of calculations which can produce a lot of objects. So , it feels kind of intuitive to not make it immutable. The situation which comes to my mind is something like of string operations and then the option of StringBuilder. Should there be non-immutable counterpart of BigInteger ? I think it might be beneficial in a lot of situations.

我知道不变性的好处以及它在许多情况下的好处.我只是想了解BigInteger带来的好处.我已经使用BigInteger来计算大数的阶乘.因此,我宁愿使用一个可变的BigInteger.同样,BigInteger将用于结果远远大于int的计算.对于其他情况,还有BigDecimal.

I know the benefits of immutability and how it's beneficial in many cases. I just wanted to understand the benefits w.r.t BigInteger. I have used BigInteger to calculate factorial of large numbers. So , I would have preferred a mutable BigInteger. Similarly , BigInteger will be used for calculations where the result is much larger than int. For other cases there is BigDecimal.

推荐答案

Effective Java by Josh Bloch explains the benefits of immutable classes, in "Item 15: Minimize Mutability":

不变的类别 比起设计,实施和使用更容易 可变的类.他们不太容易 错误并且更安全.

Immutable classes are easier to design, implement, and use than mutable classes. They are less prone to error and are more secure.

一个不可变的对象是 simple ,因为该对象只能以一种状态(即其创建时的状态)存在.简单代码的bug往往较少.由于无法修改对象,因此在没有外部同步的情况下它也是线程安全的.

An immutable object is simple, because the object can only exist in one state -- the state it was created in. Simple code tends to have fewer bugs. Because the object cannot be modified, it is also thread-safe without external synchronization.

Bloch以BigInteger为例解决不可变类的性能问题:

Bloch addresses the performance problem with immutable classes, using BigInteger as an example:

不可变类的唯一真正的缺点是它们需要一个 每个不同值的单独对象.创建这些对象可能会非常昂贵, 特别是如果它们很大. [...]

The only real disadvantage of immutable classes is that they require a separate object for each distinct value. Creating these objects can be costly, especially if they are large. [...]

在内部,不可变类可以是任意聪明的.例如,BigInteger具有一个包专用的可变伴侣类",用于加速多步操作(例如模幂). 出于前面概述的所有原因,使用可变伴侣类比使用BigInteger困难得多,但是幸运的是您不必:BigInteger的实现者为您完成了艰苦的工作.

Internally, the immutable class can be arbitrarily clever. For example, BigInteger has a package-private mutable "companion class" that it uses to speed up multistep operations such as modular exponentiation. It is much harder to use the mutable companion class than to use BigInteger for all of the reasons outlined earlier, but luckily you don’t have to: the implementors of BigInteger did the hard work for you.

因此,在内部,BigInteger已经为您进行了一些优化.如果您确实想要可变的BigInteger,则可以使用 BitSet ,但请注意,这可能会使您的代码更复杂-且更容易出错.除非您确信使用可变版本会为您带来明显的性能提升,否则您应该使用最简单的解决方案(BigInteger)(另请参见同一本书中的第55项:明智地优化").

So internally, BigInteger already does some optimization for you. If you really want a mutable BigInteger, you can use a BitSet, but note that this probably makes your code more complex -- and more error-prone. You should use the simplest possible solution (BigInteger) unless you are confident that using a mutable version will give you a noticeable performance gain (see also "Item 55: Optimize judiciously" from the same book).

这篇关于为什么Java中的BigInteger设计为不可变的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆