BigDecimal - to use new or valueOf

后端 未结 3 1521
感情败类
感情败类 2020-11-28 04:35

I came across two ways of getting BigDecimal object out of a double d.

1. new BigDecimal(d)
2. BigDecimal.valueOf(d)

Which would be a bette

3条回答
  •  囚心锁ツ
    2020-11-28 05:06

    Basically valueOf(double val) just does this:

    return new BigDecimal(Double.toString(val));

    Therefore -> yep, a new object will be created :).

    In general I think it depends upon your coding style. I would not mixure valueOf and "new", if both are the same outcome.

提交回复
热议问题