BigDecimal.valueOf
suggest changeBigDecimal caches the frequently used values 0 through 10. Prefer BigDecimal.valueOf to a constructor with the same argument. In the example below, a can be a cached instance; b is not.
BigDecimal a = BigDecimal.valueOf(10L); // may return a cached instance
BigDecimal b = new BigDecimal(10L); // does not return a cached instance
BigDecimal c = BigDecimal.valueOf(20L); // 20 is outside the cache
BigDecimal d = new BigDecimal(20L); // does not return a cached instance
// valueOf(double) is the value of Double.toString(double), so it is the decimal you wrote
BigDecimal a = BigDecimal.valueOf(15.15);
// new BigDecimal(double) is the exact binary value, which is not 15.15
BigDecimal b = new BigDecimal(15.15);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents