Am I wrong in saying that this is because of double imprecision?
You are absolutely right, this is exactly because of double
's imprecision.
But since this is a BigDecimal
, shouldn't it be the same?
No, it shouldn't. The error is introduced the moment you create new BigDecimal(0.333333333)
, because 0.333333333
constant already has an error embedded in it. At that point there is nothing you can do to fix this representation error: the proverbial horse is out of the barn by then, so it's too late to close the doors.
When you pass a String
, on the other hand, the decimal representation matches the string exactly, so you get a different result.