BigDecimal.ZERO replaces new BigDecimal(0)

I just want to share here. If we want to define some BigDecimal variables, we can first see if BigDecimal has been defined first. For example, new BigDecimal(0) can be replaced by BigDecimal.ZERO, as follows:

BigDecimal bigDecimal = BigDecimal.ZERO;

Following the BigDecimal class, you will find that some BigDecimal constant arrays have been defined
Insert picture description here
in the BigDecimal class , as follows: So in fact, in the preparation stage of the BigDecimal class loading stage, these arrays will be initialized, and they can be used normally. , You can use the BigDecimal type objects defined in these arrays as caches, and then take the objects in the cache first to avoid creating duplicate objects, which can also optimize the code.

to sum up

Using BigDecimal.ZERO to replace new BigDecimal(0) is just an introduction. What I want to express more is that when defining the BigDecimal class in the future, first see if there are any ready-made objects that can be used to avoid unnecessary operations.

Guess you like

Origin blog.csdn.net/weixin_38106322/article/details/111242305