Java 使用“new”关键字或静态方法声明和初始化大十进制数是更好的方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19586738/
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-12 18:33:57  来源:igfitidea点击:

Is it better approach to declare and initialize a bigdecimal number with "new" keyword or static method?

java

提问by Ankit Kumar

I have to declare and initialize BigDecimal wrapper object several times in project. Then which is better approach either by java Code:

我必须在项目中多次声明和初始化 BigDecimal 包装器对象。然后通过java代码哪个更好的方法:

BigDecimal num=new BigDecimal("123"); 

or in NumberUtilsclass there is already a static method available as

或者在NumberUtils课堂上已经有一个静态方法可用

public static BigInteger createBigInteger(String str) {
    if (str == null) {
        return null;
    }
    return new BigInteger(str);
}

BigDecimal num=NumberUtils.createBigInteger("123"); 

Plese tell me, which is better approach as we compared performance wise(memory and speed).

请告诉我,当我们比较性能(内存和速度)时,哪种方法更好。

采纳答案by Jon Skeet

Well clearly createBigIntegeris doing more work - it's checking for nullity, when you know the argument value won't be null anyway. That's only a tiny, tiny bit of extra work though - almost certainto be irrelevant in reality.

很明显,createBigInteger它正在做更多的工作 - 它正在检查无效性,当您知道参数值无论如何都不会为 null 时。不过,这只是一点点额外的工作——几乎可以肯定在现实中是无关紧要的。

I'd be surprised if this were really a performanceconcern anyway though - have you identified this to be a bottleneck in your code? If not, write the most readablecode - which for me would be the constructor call. Then identify what your performance requirements are, and test your whole system against them. If it's not performing well enough, write more tests or use a profiler to identify which areas are causing problems.

不过,如果这真的是一个性能问题,我会感到惊讶- 您是否确定这是代码中的瓶颈?如果没有,请编写最易读的代码——对我来说就是构造函数调用。然后确定您的性能要求是什么,并根据这些要求测试您的整个系统。如果它的表现不够好,请编写更多测试或使用分析器来确定哪些区域导致了问题。

Another alternative would be to use the BigDecimal(int)constructor - why bother parsing a string?

另一种选择是使用BigDecimal(int)构造函数 - 为什么要解析字符串?

BigDecimal num = new BigDecimal(123);

If you wanted, you could even have this as a constant, so you could reuse the object:

如果你愿意,你甚至可以把它作为一个常量,这样你就可以重用这个对象:

private static final BigDecimal DEFAULT_FOOBAR_VALUE = new BigDecimal(123);

// In a method or whatever...
BigDecimal num = DEFAULT_FOOBAR_VALUE;

Aside from performance, I'd argue this is cleareras it indicates the reason for the constant.

除了性能之外,我认为这更清楚,因为它表明了常数的原因。

回答by Tony Giaccone

You just need to show caution when using a decimal value in the initialization of a BigDecimal.

BigDecimal.

BigDecimal onePercentPlusABit = new BigDecimal(0.01);

is not equal to

不等于

BigDecimal onePercentExact = new BigDecimal("0.01");

and this:

和这个:

BigDecimal(0.01).setScale(2, RoundingMode.HALF_UP);

is significantly slower than

明显慢于

BigDecimal("0.01");

回答by Wathsala Karunathilake

You can use the following constant to initialize BigDecimal.

您可以使用以下常量来初始化 BigDecimal。

 BigDecimal.ZERO
 BigDecimal.ONE
 BigDecimal.Ten
BigDecimal average = BigDecimal.ZERO;