Java BigDecimal.ZERO 与新的 BigDecimal(0)。使用哪个,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26364667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:22:44  来源:igfitidea点击:

BigDecimal.ZERO vs. new BigDecimal(0). Which to use and why?

javabigdecimal

提问by erp

I was wondering if these two are the same. Can anyone verify? (I am trying to replace the 1st with the 2nd)

我想知道这两个是不是一样的。有人可以验证吗?(我试图用第二个替换第一个)

BigDecimal totalCurrentSales = new BigDecimal(0);

and

BigDecimal totalCurrentSales = BigDecimal.ZERO;

The reason I ask is that it is improper to declare it the first way since you are not supposed to create instances of already existing BigIntegerand BigDecimal (ZERO, ONE, TEN). So I was wondering if I could say it the second way and it still be considered creating an instance. Instead of me having to create a variable zeroor something that is equal to BigDecimal.ZERO. Or are there any other ways?

我问的原因是,以第一种方式声明它是不合适的,因为您不应该创建已经存在的BigIntegerBigDecimal (ZERO, ONE, TEN). 所以我想知道我是否可以用第二种方式说它仍然被认为是创建一个实例。而不是我必须创建一个变量zero或等于BigDecimal.ZERO. 或者还有其他方法吗?

I tried

我试过

BigDecimal totalCurrentSales = new BigDecimal(BigDecimal.ZERO);

but eclipse wasn't too happy.

但日食不太高兴。

采纳答案by bdkosher

Mathematically, they're the same. Plus, since BigDecimals are immutable, you don't need to worry about creating new instances to do new calculations. As soon as you perform some operation on your totalCurrentSalesinstance, you'll actually be creating a new BigDecimal and reassigning the totalCurrentSalesreference to the new value.

在数学上,它们是相同的。另外,由于 BigDecimals 是不可变的,因此您无需担心创建新实例来进行新计算。一旦对totalCurrentSales实例执行某些操作,您实际上将创建一个新的 BigDecimal 并将totalCurrentSales引用重新分配给新值。

From a instantiation perspective, they're not necessarily exactly the same. In the OpenJDK 6b14 implementation, for example, BigDecimal.ZEROis created by invoking the private new BigDecimal(BigInteger, long, int)constructor with values BigInteger.ZERO, 0, and 0.

从实例化的角度来看,它们不一定完全相同。在OpenJDK的6b14实现,例如,BigDecimal.ZERO通过调用私人创建的new BigDecimal(BigInteger, long, int)构造函数值BigInteger.ZERO00

From a code quality perspective, using BigDecimal.ZEROis preferable to new BigDecimal(0)as you avoid the extra instantiation and having a literal in your code.

从代码质量的角度来看,使用BigDecimal.ZEROnew BigDecimal(0)避免额外的实例化和在代码中包含文字更可取。

回答by Nathan Hughes

BigDecimal doesn't have a constructor that takes a BigDecimal as an argument, so that explains why Eclipse would not be happy with that.

BigDecimal 没有将 BigDecimal 作为参数的构造函数,因此这就解释了为什么 Eclipse 对此不满意。

BigDecimal is immutable, which means once you create an object its state never changes.

BigDecimal 是不可变的,这意味着一旦你创建了一个对象,它的状态就永远不会改变。

Also BigDecimal's equals and hashcode methods are overridden to go by value, as opposed to Object's default implementation, which compares references. So there is no difference between BigDecimal.ZERO and new BigDecimal("0") from the point of view of how they are used, except that creating a new instance is more work for the JVM (and will generate more garbage when you don't need that object any more).

BigDecimal 的 equals 和 hashcode 方法也被重写为按值,而不是 Object 的默认实现,它比较引用。所以从它们的使用方式来看,BigDecimal.ZERO 和 new BigDecimal("0") 之间没有区别,只是创建一个新实例对 JVM 来说是更多的工作(并且当你不这样做时会产生更多的垃圾)不再需要那个对象了)。

BigDecimal's being immutable and value-based means what specific reference is used won't matter to the code using the BigDecimal.

BigDecimal 的不可变和基于值意味着使用什么特定引用对使用 BigDecimal 的代码无关紧要。

Because BigDecimal.ZERO is already created for you and comparisions between BigDecimals are by value, it makes sense to minimize the number of values you use so that your programs create less garbage. That's why you're getting encouraged to use BigDecimal.ZERO.

因为已经为您创建了 BigDecimal.ZERO 并且 BigDecimals 之间的比较是按值进行的,所以最小化您使用的值的数量是有意义的,以便您的程序产生更少的垃圾。这就是为什么鼓励您使用 BigDecimal.ZERO。