SECTION 1
Historical Context & Motivation
Every program you've ever used — from a calculator app to a massive video game — relies on the computer performing arithmetic behind the scenes. The idea of encoding mathematical operations into a machine goes back centuries, well before the invention of modern programming languages like Java. Understanding how arithmetic operators evolved helps you appreciate why Java handles expressions the way it does today.
1642 • Pascal's Mechanical Calculator
1957 • FORTRAN Introduces Operator Precedence
+ and *, and it introduced a clear precedence system so the compiler would evaluate them in the correct order.1972 • C Language Sets the Standard
1995 • Java Is Released
The central question is straightforward but critically important: when a Java expression contains several operators, such as 3 + 4 * 2, how does the compiler decide which operation to perform first? If it adds before multiplying, you get 14. If it multiplies before adding, you get 11. The answer lies in operator precedence — a well-defined set of rules that every Java programmer must understand.
SECTION 2
Core Principles & Definitions
Java provides five arithmetic operators that work on numeric data types such as int and double. Each operator takes two operands (the values on either side) and produces a result. Before you can write correct expressions, you need to understand what each operator does and how Java decides the order in which they execute.
The Five Operators
+ (addition), - (subtraction), * (multiplication), / (division), and % (modulus, which gives the remainder).Precedence
Associativity
+ and -), Java evaluates them left to right. This is called left-to-right associativity.Parentheses Override
( ) to force a specific evaluation order. Expressions inside parentheses are always computed first, regardless of operator precedence.Integer vs. Floating-Point Division
/ are integers, Java performs integer division — it truncates the decimal part. For example, 7 / 2 yields 3, not 3.5.KEY TAKEAWAY
3 + 4 * 2, the multiplication is the higher-priority task — it gets done first, and then the addition uses its result.SECTION 3
Visual Explanation — Precedence Hierarchy
When you look at this hierarchy, remember that you can always move an operation to the top tier by wrapping it in parentheses. For instance, (3 + 4) * 2 forces the addition to happen before the multiplication, producing 14 instead of 11. This gives you complete control over evaluation order, no matter how complex your expression gets.
SECTION 4
How Arithmetic Operators Work in Java
The Five Operators in Detail
ADDITION
Returns the sum of a and b. Example: 10 + 3 → 13
SUBTRACTION
Returns the difference. Example: 10 - 3 → 7
MULTIPLICATION
In Java source code, use * for multiplication. Example: 10 * 3 → 30
DIVISION (INTEGER & FLOATING-POINT)
If both operands are int, the decimal portion is truncated: 10 / 3 → 3. If either operand is a double, you get a decimal result: 10.0 / 3 → 3.333...
MODULUS (REMAINDER)
Returns the remainder after integer division. Example: 10 % 3 → 1, because 10 ÷ 3 = 3 remainder 1. Very useful for checking if a number is even (n % 2 == 0).
Precedence Rule Summary
When you write a compound expression like 2 + 3 * 4 - 1, Java applies two rules to determine evaluation order. First, it checks precedence: multiplication and division go before addition and subtraction. Second, among operators of the same precedence level, it checks associativity: all arithmetic operators associate left to right. So 2 + 3 * 4 - 1 becomes 2 + 12 − 1, then 14 − 1, which gives 13.
Watch Out for Integer Division
int avg = (a + b) / 2; and being surprised that the result has no decimal part. If a and b are both integers, the division truncates. To get a decimal average, cast one operand: (double)(a + b) / 2.SECTION 5
Detailed Operator Breakdown
| Operator | Symbol | Example | Result | Precedence |
|---|---|---|---|---|
| Addition | + | 7 + 3 | 10 | Lower |
| Subtraction | - | 7 - 3 | 4 | Lower |
| Multiplication | * | 7 * 3 | 21 | Higher |
| Division | / | 7 / 3 | 2 (int) | Higher |
| Modulus | % | 7 % 3 | 1 | Higher |
Java's five arithmetic operators with precedence levels
8 + 4 * 3 - 10 / 2 % 3 through each evaluation step. Notice how all three high-precedence operators (*, /, %) are resolved left to right before the lower-precedence operators (+, -) are touched.The step-by-step diagram above is exactly how Java's compiler processes a compound expression. It scans for the highest-precedence operators first, resolves them left to right, and then moves down to the next tier. If you ever feel unsure about the order, adding parentheses costs nothing in performance and makes your code much more readable.
SECTION 6
Worked Example — Evaluating a Java Expression
Let's carefully evaluate the following Java statement and predict the value stored in result:
int result = 5 + 18 / 4 * 2 - 3 % 2;
Evaluating 5 + 18 / 4 * 2 - 3 % 2
- 1
Step 1 — Identify High-Precedence Operators
The expression contains five operators. The high-precedence group is/,*, and%. These must all be evaluated before+and-. Among the high-precedence operators, we go left to right. - 2
Step 2 — Evaluate 18 / 4 (Integer Division)
Since both 18 and 4 are integers, Java performs integer division: 18 ÷ 4 = 4.5, but the decimal is truncated.18 / 4→ 4 - 3
Step 3 — Evaluate 4 * 2
The result of Step 2 (which is 4) is now the left operand of*. So we compute 4 × 2.4 * 2→ 8 - 4
Step 4 — Evaluate 3 % 2 (Modulus)
The modulus operator returns the remainder of 3 ÷ 2. Since 2 goes into 3 once with 1 left over, the remainder is 1.3 % 2→ 1 - 5
Step 5 — Evaluate Remaining (Low Precedence, Left to Right)
The expression is now5 + 8 - 1. Evaluating left to right: 5 + 8 = 13, then 13 − 1 = 12.result= 12
Pro Tip
int result = 5 + ((18 / 4) * 2) - (3 % 2); — this produces the same answer but is much easier for another programmer (or your future self) to read.SECTION 7
Common Pitfalls & Best Practices
Even experienced programmers can trip over arithmetic operator nuances in Java. The table below highlights the most frequent mistakes alongside the recommended practices that prevent them.
| Pitfall | What Goes Wrong | Best Practice |
|---|---|---|
| Integer division surprise | 5 / 2 gives 2, not 2.5 | Use 5.0 / 2 or cast to double |
| Division by zero | Dividing an int by 0 throws ArithmeticException at runtime | Check the denominator before dividing |
| Forgetting precedence | Writing a + b / 2 expecting the average, but it divides b first | Use parentheses: (a + b) / 2 |
| Integer overflow | Large multiplications can exceed int range (≈ 2.1 billion) | Use long for large products |
| Misusing modulus with negatives | -7 % 3 gives −1 in Java (sign follows the dividend) | Test with negative values or use Math.floorMod() |
Common arithmetic pitfalls and how to avoid them
KEY TAKEAWAY
SECTION 8
Connection to Advanced Concepts
Arithmetic operators and precedence are the foundation for more advanced Java features. As you progress, you'll encounter additional operators and more complex evaluation scenarios. The table below previews how today's concepts connect to what comes next.
| This Lesson | Next Level |
|---|---|
+ adds numbers | + also concatenates Strings ("Hi" + "!" → "Hi!"). Precedence still applies when mixing numbers and Strings. |
| Precedence of arithmetic operators | Java has ~15 precedence levels covering relational (<, >), logical (&&, ||), and assignment operators. |
Simple expressions: a + b | Compound assignment operators like +=, -=, *= combine arithmetic with assignment in a single step. |
% checks remainders | Modulus becomes essential in loops, hashing algorithms, and circular data structures. |
| Casting to avoid integer division | Type promotion rules: Java automatically widens int to double in mixed expressions. Understanding the full promotion hierarchy prevents unexpected results. |
How arithmetic operators connect to more advanced Java topics
Mastering arithmetic precedence now saves you significant debugging time later. When you move into boolean expressions and conditional statements, you'll apply the same mental model — check precedence first, then associativity — to much more complex expressions.
SECTION 9
Practice Problems
Question 1: CONCEPTUAL
2 + 3 * 4 evaluates to 14, not 20, in Java. Which rule determines this?Show answer
* has higher precedence than the addition operator +, so 3 * 4 is evaluated first, yielding 12. Then 2 + 12 is computed, giving 14. If you wanted the answer to be 20, you would need to write (2 + 3) * 4 to force the addition to occur first using parentheses.Question 2: BASIC CALCULATION
x after this statement executes? int x = 15 % 4 + 6 / 2;Show answer
% and / have the same (higher) precedence, so we evaluate left to right. First, 15 % 4 gives 3 (since 15 ÷ 4 = 3 remainder 3). Next, 6 / 2 gives 3. Finally, the addition 3 + 3 yields 6. So x is 6.Question 3: INTERMEDIATE
result: int result = (10 + 2) * 3 / 4 - 5 % 3;Show answer
(10 + 2) = 12. Now the expression is 12 * 3 / 4 - 5 % 3. Among the high-precedence operators, evaluate left to right: 12 * 3 = 36, then 36 / 4 = 9, then 5 % 3 = 2. Finally, 9 - 2 = 7. The value of result is 7.Question 4: APPLIED
int grade = examScore + homeworkTotal / numAssignments * 2 - penalty; Given examScore = 80, homeworkTotal = 45, numAssignments = 10, and penalty = 5 (all int), what is the value of grade? If the teacher actually wanted the average homework score doubled and then added to the exam minus the penalty, what parenthesized version should they write?Show answer
45 / 10 = 4 (integer division), then 4 * 2 = 8. Now the low-precedence operations: 80 + 8 - 5 = 83. So grade is 83. However, if the teacher wanted the full homework average doubled and combined with the exam minus penalty, they should write: int grade = examScore + (homeworkTotal / numAssignments) * 2 - penalty; — the result is the same here (83), but for clarity and maintainability the parentheses are valuable. If the teacher also wanted to avoid integer truncation in the average, they would need a cast to double.Question 5: CRITICAL THINKING
int n = 100 / 10 / 2 * 3 % 7;. Without running the code, predict the result. Then explain: would placing parentheses around (100 / 10) change the answer? What about 100 / (10 / 2)? Why do these two placements behave differently?Show answer
100 / 10 = 10, 10 / 2 = 5, 5 * 3 = 15, 15 % 7 = 1. So n is 1. Placing parentheses around (100 / 10) does not change the answer because that division was already the first operation. However, 100 / (10 / 2) forces 10 / 2 = 5 to happen first, then 100 / 5 = 20, then 20 * 3 = 60, then 60 % 7 = 4. This changes the result because division is not associative — the order in which you divide matters. This is a key insight: parentheses only change the result when they alter the natural left-to-right evaluation.SUMMARY
Lesson Summary
Summary
Java provides five arithmetic operators: + (addition), − (subtraction), * (multiplication), / (division), and % (modulus). When multiple operators appear in an expression, Java uses operator precedence to decide the evaluation order: *, /, and % are evaluated before + and −. Operators at the same precedence level are resolved using left-to-right associativity.
You can override the default order with parentheses, which force the enclosed expression to be evaluated first. Watch out for integer division (which truncates decimals when both operands are int) and division by zero (which causes an ArithmeticException). When in doubt, use parentheses to make your intent clear — they cost nothing in performance and dramatically improve code readability.