AP COMPUTER SCIENCE A | UNIT 1: PRIMITIVE TYPES
1.3 Expressions and Assignment Statements
Study notes on Java operators, expression evaluation, integer division, modulo arithmetic, and compound assignment. Essential for mastering mathematical logic in AP CSA.
Big Idea: Arithmetic Operations
Programming isn't just about storing data; it's about manipulating it. Java uses operators (symbols like +, -, *, /) to perform calculations on values, called operands. An expression is a combination of operators and operands that evaluates to a single value.
Arithmetic Operators
Java supports the five basic arithmetic operations. Understanding their behavior with different data types (int vs double) is critical.
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
+ | Addition | Adds two values | 5 + 2 | 7 |
- | Subtraction | Subtracts right from left | 5 - 2 | 3 |
* | Multiplication | Multiplies two values | 5 * 2 | 10 |
/ | Division | Divides left by right | 5.0 / 2.0 | 2.5 |
% | Modulo (Remainder) | Returns the remainder | 5 % 2 | 1 |
The Trap: Integer Division
When you divide two integers in Java, the result is an integer. The decimal part is truncated (thrown away), not rounded.
int b = 1 / 2; // Result is 0
Mathematical equivalent:
\( \lfloor 5 \div 2 \rfloor = 2 \)
The Modulo Operator (%)
The modulo operator returns the remainder of a division. It is extremely useful for checking even/odd numbers or cycling through arrays.
4 % 2 // 0 (Even number)
Formula:
\( a \% n = a - (n \times \lfloor a/n \rfloor) \)
Interactive Java Modulo Calculator
Test how Java handles integer division and modulo operations.
Assignment & Compound Operators
The assignment operator
= has the lowest precedence. It evaluates the expression on the right first, then stores it in
the variable on the left.
Compound Assignment
Shortcuts for updating a variable based on its current value.
| Operator | Equivalent | Example |
|---|---|---|
+= | x = x + y | score += 10; |
-= | x = x - y | lives -= 1; |
*= | x = x * y | price *= 0.9; |
/= | x = x / y | half /= 2; |
%= | x = x % y | time %= 60; |
Increment & Decrement
Adding or subtracting exactly 1.
count++; // count is now 1
count--; // count is now 0
Note: In AP CSA, you only need to know the postfix
form (i++), though prefix (++i) exists in Java.
Operator Precedence
Java follows standard mathematical rules for order of operations.
- Parentheses
( )— Highest Precedence - Multiplicative
* / %— Evaluated Left to Right - Additive
+ -— Evaluated Left to Right - Assignment
= += -=— Lowest Precedence (Right to Left)
Evaluation Example
Evaluate: int result = 5 + 3 * 4 - 2;
- Multiplication first: \( 3 \times 4 = 12 \) →
5 + 12 - 2 - Addition (Left to Right): \( 5 + 12 = 17 \) →
17 - 2 - Subtraction: \( 17 - 2 = 15 \)
- Assignment:
resultbecomes 15.
Test Your Understanding
Frequently Asked Questions
Does Java follow PEMDAS?
Yes, predominantly. Parentheses first, then Multiplication/Division/Modulo, then Addition/Subtraction. Unlike standard math, operators of the same precedence are strictly evaluated Left-to-Right.
What is the result of -5 % 2?
In Java, the sign of the remainder matches the sign of
the dividend (the left number). So -5 % 2 is -1. However, 5 % -2
is 1.
Why do I get 0 when I divide 1/2?
If both operands are integers, Java performs integer
division, which truncates the decimal. 1 / 2 is 0.5, which truncates to
0. To get 0.5, use at least one double: 1.0 / 2.
Related on RevisionTown
Start here (prerequisites)
- 1.1 Why Programming? Why Java?
- 1.2 Variables and Data Types
- AP Computer Science A: Primitive Types Hub

