As I understand, I can abstract the precision away using the Number interface, which has Integer, Long and BigInteger implementations and plug the type I need depending on the range of my values. I understand the BigInteger is not ubiquitous because it takes more memory and less performant. What are the figures?
Asked
Active
Viewed 3,331 times
0
-
1Why do you say `BigDecimal` and `double` are used for different purposes? And what are you trying to contrast? – shmosel May 09 '16 at 19:22
-
`BigInteger` is, in fact, best compared to `double`. But frankly any sort of "figures" would vary hugely depending on your platform. (All this said: almost any decision about what datatype you should be using would depend on what sort of numbers you're storing, not performance needs.) – Louis Wasserman May 09 '16 at 19:24
-
3And frankly, generifying over `Number` is almost useless; there's almost no useful operations you can do with a generic `Number`. – Louis Wasserman May 09 '16 at 19:27
-
@LouisWasserman do you mean `BigDecimal`? – shmosel May 09 '16 at 19:28
-
1@shmosel whoops, yes. `BigDecimal` is best compared to `double`. – Louis Wasserman May 09 '16 at 19:33
-
`long` is more performant than `Long`, but `BigInteger` is similar to `Long` for numbers which fix in both. – Peter Lawrey May 10 '16 at 07:48
1 Answers
1
BigDecimal is similar to double, it can help maintain precision and is useful for display purposes. Use it only when needed, otherwise double is better for many considerations (memory, performance).
It's useful for instance when storing money values: you want to keep the representation, as well as round the number right when doing sums (additions).

abdelrahman-sinno
- 1,157
- 1
- 12
- 33
-
-
Alright, so you want benchmarks; I don't have these, but I agree with Louis who commented saying your criteria should be your design choice, not performance benchmarks. I'm not sure Number is very helpful to you. If you want to cut it short, just go ahead with BigDecimal, if your application isn't doing all these great operations on the values, and won't end up maxing up your CPU and memory. – abdelrahman-sinno May 10 '16 at 07:27