- Problems due to numeric overflow or rounding
- Mistakes that can occur when handling special numbers like Integer.MIN_VALUE, -0.0, or Double.NaN
- How to avoid mistakes when doing bit manipulation and using hexadecimal numbers
- Caveats in seemingly simple numerical algorithms like oddness check and clamping
- Why it’s better to avoid certain numeric data types
Modern programs usually operate with very high-level abstractions. Still, numbers and primitive math is used by programmers quite often. Unfortunately, computer math differs in many ways from real math, and these differences often produce unpleasant bugs. An experienced Java programmer should be aware about many peculiarities of number processing in this language, including numeric overflow and implicit conversions.
4.1 Mistake #25. Accidental use of octal literal
4.2 Mistake #26. Numeric overflow
4.2.1 Overflow in Java
4.2.2 Assigning the result of int multiplication to a long variable
4.2.3 File size and financial computations
4.3 Mistake #27. Rounding during integer division
4.4 Mistake #28. Absolute value of Integer.MIN_VALUE
4.5 Mistake #29. Oddness check and negative numbers
4.6 Mistake #30. Widening with precision loss
4.7 Mistake #31. Unconditional narrowing
4.8 Mistake #32. Negative hexadecimal values
4.9 Mistake #33. Implicit type conversion in compound assignments
4.10 Mistake #34. Division and compound assignment
4.11 Mistake #35. Using the short type
4.12 Mistake #36. Manually writing bit-manipulating algorithms
4.13 Mistake #37. Forgetting about negative byte values
4.14 Mistake #38. Incorrect clamping order
4.15 Mistake #39. Ignoring the difference between +0.0 and −0.0
4.16 Mistake #40. Forgetting to handle the NaN value
4.17 Summary