Java Numbers & Arithmetic Operators
Java provides standard arithmetic operators along with a powerful built-in Math library for advanced calculations. Understanding integer division and operator precedence prevents subtle math bugs.
"Integer division in Java is like cutting whole apples. If you divide 7 apples among 2 people, each gets 3 whole apples—the fractional half is discarded unless you use double!"
Deep Dive: How It Works
Basic operators: `+` (add), `-` (subtract), `*` (multiply), `/` (divide), `%` (modulus / remainder).
Integer Division Trap: Dividing two integers (e.g. `5 / 2`) produces an integer (`2`), truncating the decimal. To get `2.5`, at least one operand must be a floating point (`5.0 / 2`).
Compound operators: `+=`, `-=`, `*=`, `/=`, `%=`, `++` (increment), `--` (decrement).
Math utility class: `Math.max(a, b)`, `Math.min(a, b)`, `Math.sqrt(x)`, `Math.pow(base, exp)`, `Math.abs(x)`.
Syntax Blueprint
int sum = a + b; int remainder = a % b; double root = Math.sqrt(25.0);
Use % to find remainders (useful for even/odd checks). Use Math methods for powers and roots.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Expecting a decimal from int division: double d = 5 / 2; (results in 2.0)Why it happens: 5 / 2 evaluates to 2 as an int, which is then cast to 2.0.
How to fix: Write double d = 5.0 / 2; or (double) 5 / 2;
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Calculate rectangle area and perimeter
Given length = 8 and width = 5, calculate area (length * width) and perimeter (2 * (length + width)).
Print "Area: " + area and "Perimeter: " + perimeter on separate lines.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.