Integer Arithmetic & Overflow
Understand integer division, modulus, overflow behavior, and how to detect it.
Integer Arithmetic & Overflow
Java integers have fixed sizes. When a calculation exceeds the maximum or minimum value, it wraps around silently — no exception is thrown. Understanding this prevents subtle bugs.
Integer Ranges
Each integer type has a bounded range determined by its bit width:
byte: -128 to 127short: -32,768 to 32,767int: -2,147,483,648 to 2,147,483,647long: -9.2 × 10^18 to 9.2 × 10^18
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Long.MAX_VALUE); // 9223372036854775807
System.out.println(Byte.MAX_VALUE); // 127All lessons in this course
- The Math Class Essentials
- Integer Arithmetic & Overflow
- BigDecimal for Financial Calculations
- NumberFormat and printf