BigDecimal
and double
are very different types, with very different purposes. Java benefits from having both, and Java programmers should be using both of them appropriately.
The floating point primitives are based on binary to be both space and time efficient. They can be, and typically are, implemented in very fast hardware. double
should be used in contexts in which there is nothing special about terminating decimal fractions, and all that is needed is an extremely close approximation to a value that may be fractional, irrational, very big, or very small. There are good implementations of many trig and similar functions for double
. See java.lang.Math
for some examples.
BigDecimal
can represent any terminating decimal fraction exactly, given enough memory. That is very, very good in situations in which terminating decimal fractions have special status, such as many money calculations.
In addition to exact representation of terminating decimal fractions, it could also be used to get a closer approximation to e.g. one third than is possible with double
. However, situations in which you need an approximation that is closer than double
supplies are very rare. The closest double
to one third is 0.333333333333333314829616256247390992939472198486328125, which is close enough for most practical purposes. Try measuring the difference between one third of an inch and 0.3333333333333333 inches.
BigDecimal
is a only supported in software, does not have the support for mathematical functions that double
has, and is much less space and time efficient.
If you have a job for which either would work functionally, use double
. If you need exact representation of terminating decimal fractions, use BigDecimal
.