01 / 21
\(+\)
\(*\)
\(/\)
\(\%\)

Arithmetic Operators
& Precedence

Master how Java evaluates expressions using arithmetic operators and the order of operations that determines which calculations happen first.

Java

What Are Arithmetic Operators?

Arithmetic operators are symbols that tell Java to perform mathematical calculations on numeric values. They work on both integer types (int, long) and floating-point types (double, float).

The Five Arithmetic Operators

+ Addition
Adds two values: \( 7 + 3 = 10 \)
- Subtraction
Subtracts right from left: \( 7 - 3 = 4 \)
* Multiplication
Multiplies two values: \( 7 * 3 = 21 \)
/ Division
Divides left by right: \( 7 / 3 = 2 \) (int)
% Modulus
Returns remainder: \( 7 \mathbin{\%} 3 = 1 \)

Integer vs Double Division

The / operator behaves differently depending on whether the operands are integers or floating-point numbers. This is one of the most important distinctions in Java arithmetic.

int a = 7 / 2;       // Result: 3 (truncated!) double b = 7.0 / 2;    // Result: 3.5 double c = (double) 7 / 2; // Result: 3.5 (cast)

The Modulus Operator %

The modulus operator returns the remainder after integer division. It is essential for tasks like checking if a number is even or odd, cycling through ranges, or extracting digits.

5 5 5 2 Group 1 Group 2 Group 3 Remainder
\( 17 / 5 = \color{#818cf8}3 \) (quotient)    \( 17 \mathbin{\%} 5 = \color{#4ade80}2 \) (remainder)
Because \( 5 * 3 + 2 = 17 \)

Operator Precedence Rules

Java evaluates arithmetic expressions using a strict hierarchy. Just like in math class, multiplication, division, and modulus are evaluated before addition and subtraction.

\( ( ) \)  Parentheses
Highest
\( * \quad / \quad \% \)
Second
\( + \quad - \)
Third Same-level operators: evaluated left to right

Associativity: Left to Right

When operators have the same precedence, Java uses left-to-right associativity. This means the leftmost operation is evaluated first.

// Expression: 20 / 4 / 2 // Step 1: (20 / 4) = 5 // Step 2: 5 / 2 = 2 (integer division!) int result = 20 / 4 / 2; // Result: 2

int Arithmetic vs double Arithmetic

int Arithmetic double Arithmetic
Division \( 7 / 2 \) 3 (truncates) 3.5
Modulus \( 7 \mathbin{\%} 2 \) 1 1.0
Overflow? Yes (wraps around) Infinity
Precision Exact (within range) Approximate
Mixed operands Result is double (auto promotion)

Parentheses Override Everything

Parentheses force Java to evaluate the enclosed expression first, regardless of normal precedence. Nested parentheses are evaluated from the innermost outward.

// Without parentheses: int a = 2 + 3 * 4; // 2 + 12 = 14 // With parentheses: int b = (2 + 3) * 4; // 5 * 4 = 20 // Nested parentheses: int c = ((2 + 3) * (4 - 1)) / 5; // (5*3)/5 = 3

How to Evaluate an Expression

1
Parentheses first. Find the innermost ( ) and evaluate everything inside them.
2
Multiply, divide, modulus. Scan left to right for *, /, % and evaluate each in order.
3
Add and subtract. Scan left to right for + and - and evaluate.
4
Check types. If any operand was double, the result is a double. If all operands were int, the result is an int.

Example 1: Basic Precedence

Evaluate: int result = 10 + 6 * 2 - 4 / 2;

\( 10 + 6 * 2 - 4 / 2 \)
\( 6 * 2 = 12 \)
\( 4 / 2 = 2 \)
After step 1:
\( 10 + \color{#818cf8}12 - \color{#818cf8}2 \)
\( 10 + 12 = 22 \)
\( \color{#4ade80}22 - 2 \)
\( 22 - 2 = 20 \)

Example 2: Parentheses & Modulus

Evaluate: int result = (8 + 2) % 3 * 4 - 1;

1
Parentheses: \( (8 + 2) = 10 \)
2
Modulus (left to right): \( 10 \mathbin{\%} 3 = 1 \)
3
Multiplication: \( 1 * 4 = 4 \)
4
Subtraction: \( 4 - 1 = 3 \)

Example 3: Integer Division Trap

Evaluate: double result = 1 / 2 + 3.0 / 4;

1
Division (left to right): 1 / 2 : Both operands are int, so integer division: \( 0 \) (not 0.5!)
2
Division: 3.0 / 4 : One operand is double, so result is \( 0.75 \)
3
Addition: \( 0 \) + \( 0.75 \) = \( 0.75 \)

Automatic Type Promotion

When an arithmetic operator has operands of different types, Java automatically promotes the smaller type to the larger one before performing the calculation.

byte short int long float double Automatic Promotion Direction

Smaller types are always promoted to larger types

\( \text{int} + \text{double} \to \text{double} \quad\quad \text{int} * \text{long} \to \text{long} \)

Common Mistakes

✕ Integer Division Surprise
Writing \( 1/3 \) and expecting \( 0.333\dots \): it gives \( 0 \). Fix: use \( 1.0/3 \) or cast to double.
✕ Ignoring Precedence
Writing \( a + b * c \) when you mean \( (a + b) * c \): multiplication happens first without parentheses.
✕ Division by Zero
Integer \( n / 0 \) throws ArithmeticException. Double \( n / 0.0 \) gives Infinity or NaN.

Summary Reference

Operator Name Precedence Example
( ) Parentheses 1 (highest) \( (2+3) \to 5 \)
* Multiplication 2 \( 4 * 3 \to 12 \)
/ Division 2 \( 7 / 2 \to 3 \)
% Modulus 2 \( 7 \mathbin{\%} 2 \to 1 \)
+ Addition 3 \( 4 + 3 \to 7 \)
- Subtraction 3 \( 4 - 3 \to 1 \)

Question 1 of 5

What is the value of result after this line executes?
int result = 3 + 4 * 2;

Question 2 of 5

What does System.out.println(15 / 4); print?

Question 3 of 5

What is the value of x?
int x = 17 % 5 + 2 * 3;

Question 4 of 5

What does the following print?
System.out.println(1 / 2 + 1.0 / 2);

Question 5 of 5

What is the value of y?
int y = (10 - 2) * 3 / (1 + 3);