- Octal numerals may be used, accidentally resulting in a different numeric value. Prohibiting them completely in your codebase is a good idea.?
- Numeric overflow is one of the most annoying bugs in programming. When you have numerical calculations in your program, ask yourself whether overflow is possible and how the program should behave in this case.?
- There are some numbers that behave unusually. This includes?Integer .MIN_VALUE?(equal to itself when negated),?-0.0?(equals?+0.0), and?Double.NaN?(not equal to itself). Check whether your code handles such inputs correctly.?
- Compound assignment operators are tricky, as they may perform implicit narrowing conversion and change the evaluation order, producing a different result than expected.?
- When speaking about bytes, developers usually imagine a number between?0?and?255, but the?bytetype in Java ranges from?-128?to?127.?
- Avoid the?short?type in favor of?int?and the?float?type in favor of?double. You’ll rarely get the performance benefit or reduced memory usage of?short?and?float, but they may introduce problems due to reduced precision or overflow.?
- Many numeric algorithms, like oddness check, clamping values, and setting a bit in the number, are deceivingly simple, so developers prefer writing them inline. Yet it’s quite possible to write them incorrectly. Isolate every algorithm in a separate, well-tested method.