BigDecimal(" 0")和BigDecimal.ZERO之间有区别吗? [英] Is there a difference between BigDecimal("0") and BigDecimal.ZERO?

查看:13746
本文介绍了BigDecimal(" 0")和BigDecimal.ZERO之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于新变量的比较或初始化,它会对你使用的其中一个产生影响吗?

Either for comparisons or initialization of a new variable, does it make a difference which one of these you use?

我知道BigDecimal.ZERO是一个1.5特征,所以这是一个问题,但假设我使用1.5是否重要?

I know that BigDecimal.ZERO is a 1.5 feature, so that's a concern, but assuming I'm using 1.5 does it matter?

谢谢。

推荐答案

BigDecimal.ZERO 是一个预定义的常量,因此不必在运行时从字符串中计算为 BigDecimal(0)将是。它会更快,不需要创建新对象。

BigDecimal.ZERO is a predefined constant and therefore doesn't have to be evaluated from a string at runtime as BigDecimal("0") would be. It will be faster and won't require creation of a new object.

如果您的代码需要在1.5之前运行,那么您可以使用(备受诟病)单例模式创建一个等于 BigDecimal.ZERO 的对象。第一次使用时,它会调用 BigDecimal(0)来创建一个零对象,并在后续调用中返回该对象。否则,如果您的代码在1.5系统上运行,则您的单例对象只能返回 BigDecimal.ZERO ,而不会造成运行时损失。

If your code needs to run on pre-1.5, then you can use the (much maligned) Singleton pattern to create an object equivalent to BigDecimal.ZERO. The first time it is used, it would call BigDecimal("0") to create a zero object, and return that object on subsequent calls. Otherwise, if your code is running on a 1.5 system, your singleton object can just return BigDecimal.ZERO with no runtime penalty.

这篇关于BigDecimal(" 0")和BigDecimal.ZERO之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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