2.2 Expressions and Operators

Java Expressions, Operators, and Integer Division

Unit 2: Variables, Decisions, and Input · Lesson 2.2 · about 30 minutes · no coding experience needed

Adding one to a score sounds like the easiest thing in this course. It contains the single most confusing line a beginner meets, and a division rule that will silently give you the wrong answer for as long as you do not know it.

What you will be able to do

  • Predict the result of integer division and the remainder operator
  • Read and write `score = score + 1` and explain why it is not a contradiction
  • Use the compound assignment shorthand

Words you will need

Expression
In plain words Anything that works out to a value.
More precisely A combination of values, variables and operators that evaluates to a single value.
Operator
In plain words A symbol that does something to values.
More precisely Plus, minus, times, divide, and remainder: + - * / %
Integer division
In plain words Whole-number division. The remainder is thrown away.
More precisely Division where both operands are integers; the fractional part is truncated.

Build it step by step

Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.

  1. Step 1

    On your screen An editor showing arithmetic expressions with their results in comments.

    int a = 7 + 2;   // 9
    int b = 7 - 2;   // 5
    int c = 7 * 2;   // 14

    Nothing surprising yet. `*` is multiply, because there is no times sign on a keyboard. The right side is worked out first, and the answer goes in the box.

  2. Step 2

    On your screen The result of integer division shown as 3, with the remainder highlighted as discarded.

    int d = 7 / 2;      // 3, NOT 3.5
    int e = 7 % 2;      // 1, the remainder
    double f = 7.0 / 2; // 3.5

    Here is the one that catches everyone.

    `7 / 2` is 3. Not 3.5, not 4. When BOTH sides are whole numbers Java does whole-number division and throws the remainder away. It does not round; it truncates.

    `%` gives you that discarded remainder. 7 % 2 is 1.

    To get 3.5, at least one side has to be a decimal: `7.0 / 2`.

    This is not a quirk you can avoid. It will produce wrong numbers in a working program with no error message at all.

  3. Step 3

    On your screen A score variable being incremented, with the old and new values annotated.

    int score = 10;
    score = score + 1;   // score is now 11

    As math, `score = score + 1` is nonsense. As Java it is an instruction, and the order is what makes it work:

    1. Work out the RIGHT side using the current value: 10 + 1 is 11. 2. Put that result into the box on the LEFT.

    Read `=` as "becomes", never as "equals", and the line stops being strange.

  4. Step 4

    On your screen Compound assignment operators shown alongside their longhand equivalents.

    score += 1;   // same as score = score + 1
    score -= 5;   // same as score = score - 5
    score++;      // same as score = score + 1

    Shorthand for the same thing. You will see all three in real code, so you need to read them, and `score++` is the one you will meet most.

    They are conveniences, not new ideas. If a line confuses you, expand it back to the long form in your head.

Mistakes almost everyone makes here

These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.

Common wrong idea 7 / 2 is 3.5, or rounds to 4.

What is actually true It is 3. Integer division truncates, and never rounds.

Why it matters This is the highest-value fact in the lesson. It produces wrong answers with no error, which is the worst kind of bug for a beginner to face.

Common wrong idea `score = score + 1` is a contradiction and cannot be true.

What is actually true `=` means "becomes", not "equals". Right side first, then store.

Why it matters Students who read = as equals get stuck permanently on the single most common line in game code.

Common wrong idea Storing 3.5 in an int rounds it to 4.

What is actually true Java refuses it outright, and if you force it you get 3.

Why it matters Expecting rounding hides a whole class of off-by-one bugs.

Check your understanding

Answer these before moving on. They are graded instantly and you can retry.

  1. 1. What is the value of `9 / 2` in Java, when both are ints?

  2. 2. What is `9 % 2`?

  3. 3. int score = 4; score = score + 3; What is score?

  4. 4. Which expression gives 4.5?

  5. 5. `lives--;` does the same as which line?

Fill in the code

A coin is collected. Add one to the score, then work out how many complete rows of 10 coins the player has, and how many are left over.

score = score  1;
int fullRows  = score  10;
int leftOver  = score  10;
Stuck? Open a hint for each blank
  • Blank 1: One more than it was.
  • Blank 2: How many complete tens fit in. Whole-number division does this for you.
  • Blank 3: What is left after taking out the complete tens.

Lesson quiz

This one counts toward your progress. Take it when the section above makes sense.

  1. 1. int x = 15 / 4; What is x?

  2. 2. Which is the best way to test whether an int n is even?

  3. 3. int a = 3; a += 4; a++; What is a?

  4. 4. Why is `score = score + 1;` not a contradiction?

  5. 5. A game divides 7 coins evenly between 2 players using `int each = 7 / 2;`. What goes wrong?

The short version

  • Integer division TRUNCATES. 7 / 2 is 3.
  • % gives the remainder, and is how you test for even or wrap a value around.
  • Read `=` as "becomes". Right side first, then store.
  • += and ++ are shorthand for the same thing.

If you get stuck

These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]