Arithmetic Operators
& Precedence
Master how Java evaluates expressions using arithmetic operators and the order of operations that determines which calculations happen first.
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
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.
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.
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.
Associativity: Left to Right
When operators have the same precedence, Java uses left-to-right associativity. This means the leftmost operation is evaluated first.
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.
How to Evaluate an Expression
( ) and evaluate everything inside them.*, /, % and evaluate each in order.+ and - and evaluate.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;
Example 2: Parentheses & Modulus
Evaluate: int result = (8 + 2) % 3 * 4 - 1;
Example 3: Integer Division Trap
Evaluate: double result = 1 / 2 + 3.0 / 4;
1 / 2 : Both operands are int, so integer division: \( 0 \) (not 0.5!)3.0 / 4 : One operand is double, so result is \( 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.
Common Mistakes
double.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?
15 and 4 are int literals. Integer division truncates the decimal: \( 15 / 4 = 3 \) with remainder \( 3 \). The printed result is 3, not 3.75.Question 3 of 5
What is the value of x?int x = 17 % 5 + 2 * 3;
% and * have the same precedence and are evaluated left-to-right. First: \( 17 \mathbin{\%} 5 = 2 \). Then: \( 2 * 3 = 6 \). Finally: \( 2 + 6 = 8 \).Question 4 of 5
What does the following print?System.out.println(1 / 2 + 1.0 / 2);
1/2 is integer division: result is 0. 1.0/2 is double division: result is 0.5. Adding: \( 0 + 0.5 = 0.5 \). The int 0 is promoted to double for the addition.Question 5 of 5
What is the value of y?int y = (10 - 2) * 3 / (1 + 3);