Unit 1 - Primitive Types

1.3 Expressions and Assignment Statements – AP Computer Science A | RevisionTown

Master Java arithmetic operators, integer division, and the modulo operator. Free AP Computer Science A study notes with an interactive calculator and practice quiz.
Educational featured image for AP Computer Science A topic 1.3: Expressions and Assignment Statements on RevisionTown, showing Java code example with operators and syntax highlights.

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.

OperatorNameDescriptionExampleResult
+AdditionAdds two values5 + 27
-SubtractionSubtracts right from left5 - 23
*MultiplicationMultiplies two values5 * 210
/DivisionDivides left by right5.0 / 2.02.5
%Modulo (Remainder)Returns the remainder5 % 21

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 a = 5 / 2; // Result is 2
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.

10 % 3 // 1 (10 / 3 = 3 R 1)
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.

OperatorEquivalentExample
+=x = x + yscore += 10;
-=x = x - ylives -= 1;
*=x = x * yprice *= 0.9;
/=x = x / yhalf /= 2;
%=x = x % ytime %= 60;

Increment & Decrement

Adding or subtracting exactly 1.

int count = 0;
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.

  1. Parentheses ( ) — Highest Precedence
  2. Multiplicative * / % — Evaluated Left to Right
  3. Additive + - — Evaluated Left to Right
  4. Assignment = += -= — Lowest Precedence (Right to Left)

Evaluation Example

Evaluate: int result = 5 + 3 * 4 - 2;

  1. Multiplication first: \( 3 \times 4 = 12 \) → 5 + 12 - 2
  2. Addition (Left to Right): \( 5 + 12 = 17 \) → 17 - 2
  3. Subtraction: \( 17 - 2 = 15 \)
  4. Assignment: result becomes 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.

Shares: