What’s the common practice to deal with Integer overflows like 999999*999999 (result > Integer.MAX_VALUE) from an Application Development Team point of view?
One could just make BigInt mandatory and prohibit the use of Integer, but is that a good/bad idea?
If it is extremely important that the integer not overflow, you can define your own overflow-catching operations, e.g.:
You may be able to use the enrich-your-library pattern to turn this into operators; if the JVM manages to do escape analysis properly, you won’t get too much of a penalty for it:
For example:
If it is not extremely important, then standard unit testing and code reviews and such should catch the problem in the large majority of cases. Using
BigIntis possible, but will slow your arithmetic down by a factor of 100 or so, and won’t help you when you have to use an existing method that takes anInt.