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.
+ and *, and it introduced a clear precedence system so the compiler would evaluate them in the correct order.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.
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.3 + 4 * 2, the multiplication is the higher-priority task — it gets done first, and then the addition uses its result.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.
How Arithmetic Operators Work in Java
The Five Operators in Detail
a and b. Example: 10 + 3 → 1310 - 3 → 7* for multiplication. Example: 10 * 3 → 30int, the decimal portion is truncated: 10 / 3 → 3. If either operand is a double, you get a decimal result: 10.0 / 3 → 3.333...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.
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.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 |
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.
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;
/, *, and %. These must all be evaluated before + and -. Among the high-precedence operators, we go left to right.18 / 4 → 4*. So we compute 4 × 2.4 * 2 → 83 % 2 → 15 + 8 - 1. Evaluating left to right: 5 + 8 = 13, then 13 − 1 = 12.result = 12int 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.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() |
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. |
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.
Practice Problems
2 + 3 * 4 evaluates to 14, not 20, in Java. Which rule determines this?x after this statement executes? int x = 15 % 4 + 6 / 2;result: int result = (10 + 2) * 3 / 4 - 5 % 3;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?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?Lesson 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.