JAVA • SYNTAX AND CORE TYPES

Arithmetic Operators & Precedence — Use arithmetic operators and precedence

Master the five arithmetic operators in Java and learn how precedence rules determine the order of evaluation.

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
Blaise Pascal invented the Pascaline, a mechanical device that could add and subtract numbers using interlocking gears. It was one of the first machines to automate arithmetic.
1957
FORTRAN Introduces Operator Precedence
IBM's FORTRAN language was among the first to let programmers write math expressions with familiar symbols like + and *, and it introduced a clear precedence system so the compiler would evaluate them in the correct order.
1972
C Language Sets the Standard
Dennis Ritchie's C language defined the operator precedence rules and the modulus operator (%) that nearly every language after it adopted, including Java.
1995
Java Is Released
James Gosling and his team at Sun Microsystems released Java, inheriting C's arithmetic operators and precedence rules while adding strong type safety to prevent common errors.

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.

1

The Five Operators

Java's arithmetic operators are + (addition), - (subtraction), * (multiplication), / (division), and % (modulus, which gives the remainder).
2

Precedence

Operator precedence determines which operator in a compound expression is evaluated first. Multiplication, division, and modulus have higher precedence than addition and subtraction — just like in algebra class.
3

Associativity

When two operators share the same precedence (e.g., + and -), Java evaluates them left to right. This is called left-to-right associativity.
4

Parentheses Override

You can always use parentheses( ) to force a specific evaluation order. Expressions inside parentheses are always computed first, regardless of operator precedence.
5

Integer vs. Floating-Point Division

When both operands of / are integers, Java performs integer division — it truncates the decimal part. For example, 7 / 2 yields 3, not 3.5.
✦ KEY TAKEAWAY
Think of operator precedence like a to-do list with priority levels. If your boss tells you to "answer the phone and finish the report by noon," you know the noon deadline applies to the report — not to both tasks. Similarly, in 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

The diagram shows three tiers. Parentheses sit at the top with the highest priority. Multiplication, division, and modulus come next. Addition and subtraction are evaluated last. Within the same tier, Java reads left to right.

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

ADDITION
result = a + b
Returns the sum of a and b. Example: 10 + 3 → 13
SUBTRACTION
result = a − b
Returns the difference. Example: 10 - 3 → 7
MULTIPLICATION
result = a × b
In Java source code, use * for multiplication. Example: 10 * 3 → 30
DIVISION (INTEGER & FLOATING-POINT)
result = a / b
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)
result = a % b
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
One of the most common bugs for beginners is writing 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

Java's five arithmetic operators with precedence levels
OperatorSymbolExampleResultPrecedence
Addition+7 + 310Lower
Subtraction-7 - 34Lower
Multiplication*7 * 321Higher
Division/7 / 32 (int)Higher
Modulus%7 % 31Higher
This diagram traces the expression 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;

Evaluating 5 + 18 / 4 * 2 - 3 % 2
1
Step 1 — Identify High-Precedence OperatorsThe 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 * 2The 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 now 5 + 8 - 1. Evaluating left to right: 5 + 8 = 13, then 13 − 1 = 12.
result = 12
💡 Pro Tip
When in doubt, add parentheses to make your intent explicit. The same expression could be written as 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.

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.

Common arithmetic pitfalls and how to avoid them
PitfallWhat Goes WrongBest Practice
Integer division surprise5 / 2 gives 2, not 2.5Use 5.0 / 2 or cast to double
Division by zeroDividing an int by 0 throws ArithmeticException at runtimeCheck the denominator before dividing
Forgetting precedenceWriting a + b / 2 expecting the average, but it divides b firstUse parentheses: (a + b) / 2
Integer overflowLarge 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()
✦ KEY TAKEAWAY
Think of parentheses as safety rails on a mountain road. The road (your expression) might be perfectly safe without them if you know every curve, but adding guardrails (parentheses) protects you from a costly mistake — and they make the road easier for everyone else to navigate too.

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.

How arithmetic operators connect to more advanced Java topics
This LessonNext Level
+ adds numbers+ also concatenates Strings ("Hi" + "!" → "Hi!"). Precedence still applies when mixing numbers and Strings.
Precedence of arithmetic operatorsJava has ~15 precedence levels covering relational (<, >), logical (&&, ||), and assignment operators.
Simple expressions: a + bCompound assignment operators like +=, -=, *= combine arithmetic with assignment in a single step.
% checks remaindersModulus becomes essential in loops, hashing algorithms, and circular data structures.
Casting to avoid integer divisionType 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

PROBLEM 1 — CONCEPTUAL
Explain in your own words why 2 + 3 * 4 evaluates to 14, not 20, in Java. Which rule determines this?
PROBLEM 2 — BASIC CALCULATION
What value is stored in x after this statement executes? int x = 15 % 4 + 6 / 2;
PROBLEM 3 — INTERMEDIATE
Evaluate the following expression step by step and give the final value of result: int result = (10 + 2) * 3 / 4 - 5 % 3;
PROBLEM 4 — APPLIED
A teacher calculates a student's grade using the following formula: 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?
PROBLEM 5 — CRITICAL THINKING
Consider the expression 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.

Varsity Tutors • Java • Arithmetic Operators & Precedence