I am curious to understand why do we see BigDecimal.Zero but not Double.Zero, so if we need to assign a double variable as Zero then we need to cast it to (double) 0.
Any specific reasons creators added a special zero variable for bigdecimal.
I am curious to understand why do we see BigDecimal.Zero but not Double.Zero, so if we need to assign a double variable as Zero then we need to cast it to (double) 0.
Any specific reasons creators added a special zero variable for bigdecimal.
Copyright © 2021 Jogjafile Inc.
No. This compiles:
so does this:
The first one works because assignment contexts allow a widening primitive conversion (from
inttodouble).If you're talking about the
Doublewrapper, this also compiles:There is a zero value for
doublethat you can easily write - in fact, more easily thanBigDecimal.ZERO- it's just written0.0. And because assignment contexts allow a boxing conversion, you can assign0.0to the wrapperDoubletoo.BigDecimal.ZEROis not the rule - it's the exception. The compiler knows about all the primitive numeric types, and their "zeroes" are baked into the language -0,0L,0.0,0.0fetc.The compiler doesn't know about
BigDecimal.BigDecimalis just a part of the JDK, the standard library, not a part of the Java language.The language specifications does not say that there is a conversion from
doubletoBigDecimalin an assignment context, so you can't doAnd that's why there is
BigDecimal.ZERO.