I'm still learning Java, and I've been reading articles on several sites. I found an article at Java Code Geeks that I have a question about. The article is explaining open/closed principle. The article uses a scenario of applying a discount to a company's product for it's example. The first part of the code is as follows:
import java.math.BigDecimal; import java.math.RoundingMode; public class Discount { public BigDecimal apply(BigDecimal price) { BigDecimal percent = new BigDecimal("0.10"); BigDecimal discount = price.multiply(percent); return price.subtract(discount.setScale(2, RoundingMode.HALF_UP)); } } The second part of the code is as follows:
import java.math.BigDecimal; public class DiscountService { public BigDecimal applyDiscounts(BigDecimal price,Discount discount) { BigDecimal discountPrice = price.add(BigDecimal.ZERO); discountPrice = discount.apply(discountPrice); return discountPrice; } } On Oracle's site, it says that the ZERO in BigDecimal has a a value of 0 and a scale of zero. Does this mean that in price.add(BigDecimal.ZERO) we are simply adding 0 to the price that's brought in? If so, why? Or is it there simply to drop the decimal places from the price? Or is there some other purpose for it?
Thanks!