Variables, Memory Diagrams, and Arithmetic

Declaring a variable, assigning to it, and drawing the result as a memory diagram — then printing variables and combining them with arithmetic operators, including the integer division that surprises everyone the first time. Follows Think Java 2e, Chapter 2 (Variables and Operators), Sections 2.1-2.5, pp. 17-23, cross-referenced against The Java Tutorials — Variables.

Subject: Java · 65 slides · code lesson

Open the interactive version of this deck

What this lesson covers

The lesson, slide by slide

1. Variables, Memory Diagrams, and Arithmetic

Title

Think Java 2e · Chapter 2 · Variables and Operators

Sections 2.1-2.5 · pp. 17-23

2. What you will be able to do

Objectives

This lesson follows Think Java 2e, Chapter 2 (Variables and Operators), Sections 2.1-2.5, pp. 17-23. Everything on these slides can be checked against those pages.

1. Declare a variable with a type and a name, and say what each of the two parts is for.

2. Distinguish a declaration from an assignment, and explain what 'initialise' means.

3. Draw a memory diagram for a short fragment, and explain why a = b is not a statement of equality.

4. Print the value of a variable and the name of a variable, and say why one needs quotation marks.

5. Evaluate an expression the way Java does: replace each variable by its value, then apply the operators.

6. Predict the result of integer division, and say why 59 / 60 is 0.

3. Retrieve before you read

Warm-up

Thirty seconds on Chapter 1, because this lesson adds to it rather than replacing it.

Discussion prompt

From memory: what is the difference between print and println? And what does a semicolon do at the end of a line?

Hint: One of them appends something invisible.

Answer:

println appends a newline character; print does not. The semicolon ends a statement — and nearly every statement in this lesson will end with one, including the new kind you are about to meet.

Both come back immediately: Section 2.4 is about printing variables, and every declaration and assignment in this lesson is a statement that needs its semicolon.

4. The chapter in one picture

Concept

Chapter 1's programs could only print fixed text. This chapter gives you somewhere to put a value, so a program can compute something and hold on to the answer. Everything else in the book is built on that.

Figure (svg): A memory diagram showing two named boxes, hour holding 11 and minute holding 59, each labelled with type int

From here on, the examples often omit the class and method definitions to stay concise. Assume every fragment lives inside a main method like the one you wrote in Chapter 1.

Downey & Mayfield, Think Java, 2nd edition (Green Tea Press / O'Reilly, 2020) — Think Java 2e, Chapter 2 (Variables and Operators), Sections 2.1-2.5, pp. 17-23 — Chapter 2 opens on printed page 17.

5. Declaring variables

Section

Section 2.1

6. A variable is a named location in memory

Concept

A variable is a named location in memory that stores a value. To store a value you first have to declare the variable, which means saying what kind of values it may hold and what it is called.

String message;
int x;
String firstName;
String lastName;
int hour, minute;
declarationtypename(s)
String message;Stringmessage
int x;intx
String firstName;StringfirstName
int hour, minute;inthour and minute — two at once

variable — A named location in memory that stores a value.

declaration — A statement that creates a variable and gives it a type.

type — What kind of values a variable can store — int for integers like 1 and -5, char for characters like 'A' and 'z', String for text.

The last line shows how to declare several variables of the same type at once. Each declaration statement ends with a semicolon.

7. The two halves of a declaration

Notation

Every declaration you will write for the rest of this book has this shape. Naming the parts now means never having to guess later.

Annotate

  • The type comes first. It decides what kinds of value this box may hold, and Java checks every assignment against it.
  • Some types start with a capital letter and some do not. int and char are lowercase; String is capitalised. You will learn why later, but take care to get it right now — there is no such type as Int or string.
  • The name is yours to choose, but choose one that says what the variable means. firstName is better than x everywhere except a throwaway example.
  • Variable names start lowercase, unlike class names such as Hello. When a name contains more than one word, capitalise each subsequent word: firstName.
  • Names are case-sensitive. firstName, firstname and FirstName are three different variables, and mixing them up produces a 'cannot find symbol' error.
  • About 50 words are reserved — public, class, static, void, int and others. These are keywords, and you cannot use them as variable names.

You do not have to memorise the keyword list. Your editor colours them differently, and the compiler will complain if one sneaks past you both.

8. Choosing types and names for a real problem

Worked example

You are writing a program that records a student's name and the score they got on a test out of 100. Decide the declarations before writing anything else.

String studentName;
int score;
what you are storingtypewhy that type
a person's nameStringtext — a sequence of characters
a whole-number scoreintscores out of 100 are integers; no decimal places needed

Ask what kind of value each thing is.

Why: A name is text; a score is a whole number. The kind of value chooses the type.

Pick names that say what they mean.

Why: studentName and score — not s and n, which will be meaningless in a week.

Follow the conventions: lowercase first letter, capital for each later word.

Why: studentName, not StudentName (that looks like a class) and not studentname (harder to read).

Check that neither name is a keyword.

Why: Neither is. If you had wanted to call something class or int, the compiler would refuse.

End each declaration with a semicolon.

Why: A declaration is a statement, and statements end with semicolons.

Verify: Compile a program containing just these two declarations; it should compile cleanly and do nothing.

Why: If you get 'cannot find symbol', check the capitalisation of String — it is the one type here that takes a capital letter.

9. Legal variable name or not?

Definition probe

Apply the naming rules: no keywords, and the conventions for capitalisation.

Sort into buckets

Sort each proposed variable name.

legal and conventional
firstName; hour
legal but against convention
FirstName
illegal — a keyword
class; int
good
Starts with a lowercase letter, capitalises each later word, and is not reserved.
legal
The compiler accepts it, but a leading capital is the convention for CLASS names, so it will mislead a reader.
illegal
One of the roughly 50 reserved keywords the compiler uses to analyse the structure of your program. It cannot be a variable name.

10. Why the type has to be written down

Concept

Some languages let you write x = 5 with no type at all. Java makes you say int x = 5. The extra word is doing real work.

because the type is written downJava can...
it knows message holds textreject message = 123; at compile time, before the program ever runs
it knows hour holds an integerchoose integer arithmetic for hour * 60
it knows exactly how much room is neededreserve the right amount of memory for the box
it knows what operations make senseallow hour - 1 but reject subtracting one name from another

The theme to notice: the type lets errors be caught by the compiler rather than by a user. That is the trade Java is making, and it is why the language feels wordy at first and reassuring later.

11. String is capitalised; int is not

Trap

The trap

What gets typed by someone who has not yet noticed the pattern.

string message;
Int hour;
INT minute;
writtenwhat Java looks forresult
stringa type named stringcannot find symbol — no such type
Inta type named Intcannot find symbol — no such type
INTa type named INTcannot find symbol — no such type

All three produce the same unhelpful message. Java is not correcting your capitalisation; it is looking for a type with that exact name and finding nothing.

The fix

The correct forms, and the pattern behind them.

String message;
int hour;
char initial;
double ratio;
typecapitalised?the pattern
intnoa built-in primitive type
charnoa built-in primitive type
doublenoa built-in primitive type
Stringyesa class from the Java library

The pattern will make full sense in Chapter 9: lowercase types are primitives built into the language, and capitalised ones are classes. For now, the rule 'String is the odd one out' is enough.

12. How many variables does this declare?

Prediction

Read the punctuation carefully.

int hour, minute;
String firstName;
statementvariables created
int hour, minute;two — hour and minute, both int
String firstName;one

Predict first

How many variables exist after these two statements?

  • Three
  • Two
  • Four
  • One

Correct: Three

Why: The first statement declares two variables of the same type by separating the names with a comma, and the second declares one more — three in total. The comma form is a convenience for same-typed variables; the type is written once and applies to every name in the list.

13. Match the value to the type that holds it

Matching

Three of the types named in Section 2.1.

Match the pairs

  • a. -5
  • b. 'z'
  • c. "Hello!"
  • r1. int
  • r2. char
  • r3. String

Why: int stores integers like 1 and -5; char stores a single character in single quotes; String stores text in double quotes. The quotation marks are the tell — single quotes mean one character, double quotes mean a string, and no quotes at all means a number.

14. Why must names be case-sensitive?

Socratic

It causes beginners real trouble. Ask whether the alternative would be better.

Discussion prompt

firstName and firstname are different variables. Suppose Java ignored case instead, so both names meant the same box. What would that make easier, and what would it make worse?

Hint: Think about a typo, and about whether you would want it to be silently accepted.

Answer:

It would make typos harmless — firstname would just work. But it would also make them invisible, and it would forbid you from ever having two variables whose names differed only in case.

The deeper reason is consistency: Java is case-sensitive everywhere — System and system, main and Main, String and string. One rule applied uniformly is easier to hold in your head than a rule with exceptions, even when the uniform rule is occasionally annoying.

15. Assigning values

Section

Section 2.2

16. Declaring makes the box; assigning fills it

Concept

Now that the variables exist, we can store values in them with an assignment statement. The idea is straightforward even though the vocabulary can be confusing.

message = "Hello!";   // give message the value "Hello!"
hour = 11;            // assign the value 11 to hour
minute = 59;          // set minute to 59
statementthe variableits new value
message = "Hello!";message"Hello!"
hour = 11;hour11
minute = 59;minute59

The three comments show different ways people talk about the same operation — give, assign, set. They all mean the same thing.

17. Declare, then assign, then re-assign

Picture it

Watch one box over three statements.

Figure (svg): A trace strip showing a variable being declared with no value, then assigned 11, then re-assigned 12

The third step is the one to notice: assigning a new value replaces the old one. There is no history in the box — only whatever was put there most recently.

18. Declaring and initialising on one line

Worked example

You can declare a variable and assign it a value later, as in the example above. You can also do both on the same line, which is what you will normally want.

String message = "Hello!";
int hour = 11;
int minute = 59;
this one linedoes the work of
String message = "Hello!";String message; then message = "Hello!";
int hour = 11;int hour; then hour = 11;
int minute = 59;int minute; then minute = 59;

Write the type and name as usual.

Why: That part is the declaration, unchanged.

Add = and the value before the semicolon.

Why: That part is the assignment, folded into the same statement.

Prefer this form when you know the value already.

Why: It makes it impossible to forget to initialise, which is a real compile error.

Verify: Print each variable and confirm you get Hello!, 11 and 59.

Why: Then try removing one of the initialisers and using that variable — the compiler will refuse, saying the variable may not have been initialised. That refusal is a feature.

Variables must be initialised — assigned for the first time — before they can be used.

19. Declaration, assignment, or both?

Discrimination

Three kinds of statement, distinguished by whether a type appears and whether a value does.

Sort into buckets

Sort each statement.

declaration only
int hour;; String message;
assignment only
hour = 11;; message = "Hi";
declaration and assignment
int minute = 59;
decl
A type and a name, with no value — it creates the storage location and leaves it uninitialised.
assign
A name and a value, with no type — the variable already exists, and this updates what is in it.
both
A type, a name and a value in one statement. This is the form to prefer when the value is already known.

20. A variable's type constrains what may go in it

Concept

As a general rule, a variable has to have the same type as the value you assign to it. You cannot store a string in minute or an integer in message.

Figure (svg): A rule card showing that assigning the string 123 to a String is legal but assigning the integer 123 is not

assignmentlegal?why
String message = "123";yesa String variable, given a string
String message = 123;noa String variable, given an integer
int minute = 59;yesan int variable, given an integer
int minute = "59";noan int variable, given a string

Some strings look like integers but are not. "123" is made up of the characters '1', '2' and '3'; the integer 123 is a number you can do arithmetic with. Chapter 3 shows how to convert between them.

21. Using a variable before initialising it

Trap

The trap

Declared, but never given a value — and then used.

int hour;
System.out.println(hour);
statementstate of hourresult
int hour;declared, no valuefine so far
println(hour);still no valuecompile error: variable hour might not have been initialized

Note that this is a compile-time error, not a crash at run time. Java refuses to build a program that could read an empty box.

The fix

Initialise before use — ideally on the declaration line itself.

int hour = 11;
System.out.println(hour);
statementstate of hourresult
int hour = 11;declared and initialised to 11fine
println(hour);11displays 11

Declaring and initialising on one line makes this error impossible to commit. That is a good reason to prefer it whenever you already know the value.

22. Which of these will not compile?

Prediction

One assignment breaks the type rule.

String a = "123";
int b = 123;
String c = 123;
int d = 59;
statementvariable typevalue type
String a = "123";Stringstring — matches
int b = 123;intinteger — matches
String c = 123;Stringinteger — does NOT match
int d = 59;intinteger — matches

Predict first

Which line is a compile error?

  • String c = 123;
  • String a = "123";
  • int b = 123;
  • int d = 59;

Correct: String c = 123;

Why: A String variable cannot be given an integer value. The first line is fine because "123" in quotes is a string that happens to look numeric — it is the three characters 1, 2 and 3, not the number. The quotation marks are what decide the type, not the characters inside them.

23. Fold two statements into one

Fill the middle

Rewrite a declaration and an assignment as a single statement.

Fill in the blanks

// was: int score; then score = 87;
int score = 87;

Why: The type goes at the front and the assignment operator joins the name to its initial value: int score = 87;. This single statement declares the variable and initialises it, which is both shorter and safer than the two-statement form.

24. Is there ever a reason to declare without assigning?

Counterexample

You have just been told to prefer the one-line form. Push back on it.

Discussion prompt

If declaring and initialising together prevents a whole class of error, why does Java allow you to declare a variable without giving it a value at all? Try to think of a situation where you would have to.

Hint: What if the value depends on something the program has not worked out yet?

Answer:

Because sometimes the value is not known at the point where the variable has to exist. You will meet this properly in Chapter 5: a variable declared before an if statement, and given one value or another depending on which branch runs.

So the guidance is not 'always initialise on the declaration line' — it is 'initialise as soon as you know the value'. Java's compile-time check means you cannot get this wrong silently, which is why the language can afford to allow the split form at all.

25. Memory diagrams, and why = is not equality

Section

Section 2.3

26. Assignment is not equality

Concept

Because Java uses the = symbol for assignment, it is tempting to read a = b as a statement of equality. It is not. Two properties of mathematical equality fail here.

Figure (svg): A rule card contrasting mathematical equality, which is commutative and permanent, with Java assignment which is neither

property of equalityin mathematicsin Java assignment
commutativeif a = 7 then 7 = aa = 7; is legal, 7 = a; is not
permanentif a = b now, alwaysa and b can be made equal and then diverge
what may go on the leftany expressiona variable name — a storage location — only

Read a = 7 as a command — put 7 into a — rather than as a claim. That reading is correct, and it makes every later assignment obvious.

27. Watching two variables stop being equal

Picture it

Three statements. After the second, a and b hold the same value; after the third, they do not — and nothing touched b.

Figure (svg): A memory diagram showing variable a holding 3 and variable b holding 5 after a was reassigned

int a = 5; int b = a; a = 3; — the third line changes a, but it does not change b. The assignment b = a copied the value 5 at that moment; it did not tie the two boxes together.

28. Tracing a fragment with a memory diagram

Worked example

This is the technique you will use for the rest of the book: keep a table of every variable and update it one statement at a time.

int a = 5;
int b = a;
a = 3;
int c = 0;
after this statementabc
int a = 5;5——
int b = a;55—
a = 3;35—
int c = 0;350

Give every variable a column as it is declared.

Why: A dash means the variable does not exist yet.

For int b = a;, look up a's CURRENT value and copy it.

Why: a is 5, so b becomes 5. The link between them ends there.

For a = 3;, change only a's column.

Why: Nothing else on the row changes, because nothing else was assigned.

Read the final row as the memory diagram.

Why: a is 3, b is 5, c is 0 — three boxes, three values.

Verify: Print all three and expect 3, 5, 0.

Why: If you predicted b would be 3, you read b = a as a permanent link rather than a one-time copy — which is exactly the misconception this section exists to break.

29. What does this print?

Prediction

Trace it with a table before answering.

int x = 10;
int y = x;
x = 20;
y = y + 1;
System.out.println(x + " " + y);
afterxy
int x = 10;10—
int y = x;1010
x = 20;2010
y = y + 1;2011

Predict first

What is displayed?

  • 20 11
  • 20 21
  • 10 11
  • 20 10

Correct: 20 11

Why: y was given a copy of x's value (10) and then had 1 added to it, giving 11. Reassigning x to 20 later does not affect y at all, because the copy was made once, at the moment int y = x; ran. The last line reads the current contents of both boxes: 20 and 11.

30. State, and why a diagram shows only one moment

Concept

Taken together, the variables in a program and their current values make up the program's state. A memory diagram shows the state at one particular point in time.

Figure (svg): A memory diagram of three variables a, b and c holding 3, 5 and 0

state — The variables in a program together with their current values.

memory diagram — A drawing of the state: each variable is a box with its name outside and its current value inside.

As the program runs the state changes, so a diagram is a snapshot, not a summary. When you draw one, always write down which statement you drew it after — a diagram with no timestamp cannot be checked.

31. Reading b = a as a permanent link

Trap

The trap

The misconception. b = a makes b follow a, so changing a changes b too.

int a = 5;
int b = a;
a = 3;
System.out.println(b);   // predicted: 3
statementwhat the misconception predictswhat really happens
int b = a;b is now linked to ab is given a copy of a's current value, 5
a = 3;b follows and becomes 3only a changes
println(b);prints 3prints 5

This misreading comes straight from mathematics, where an equation states a relationship that holds for all time. Java's = states nothing; it does something, once.

The fix

Assignment copies a value, at the moment it runs.

int a = 5;
int b = a;   // b gets a COPY of 5
a = 3;       // only a changes
System.out.println(b);   // 5
afterab
int a = 5;5—
int b = a;55
a = 3;35

Say it as a command: put a copy of what is currently in a into b. Nothing about that sentence suggests a lasting connection — which is why the command reading is the useful one.

32. Read the memory diagram, find the wrong claim

Error analysis

A student annotated their own trace of the fragment below. One annotation is right and three are wrong.

Annotate

  • 'After line 2, a and b are the same variable.' — Wrong. They are two separate boxes that happen to hold the same value. Two boxes, one value each.
  • 'After line 3, b is 3.' — Wrong. Line 3 assigns only to a. Nothing on that line mentions b.
  • 'Line 2 is both a declaration and an assignment.' — Right. int b = a; creates b and gives it a value in one statement.
  • 'You could write line 3 as 3 = a;' — Wrong. The left side of an assignment must be a variable name, because it names the storage location being written to. 3 is not a location.

Three of the four errors come from reading = as equality. That single misreading is responsible for most confusion in this chapter.

33. Which statement about assignment survives?

Two truths and a lie

Four claims about the = operator.

Eliminate the wrong options

Eliminate the false claims and keep the true one.

  • A. a = b and b = a do the same thing.
  • B. The left side of an assignment must be a variable name.
  • C. Once a = b, the two stay equal.
  • D. 7 = a; is a legal way to give a the value 7.

Survives elimination: B

Why: The left side of an assignment names the storage location being written to, so it has to be a variable. Every other claim here comes from importing a property of mathematical equality — commutativity or permanence — that assignment simply does not have.

34. Say what a memory diagram is for

Explain it to yourself

One sentence, in your own words.

Discussion prompt

Explain what a memory diagram shows and — more importantly — what it deliberately does not show. Why does that limitation matter when you use one to find a bug?

Hint: Think about time.

Answer:

It shows the state: every variable that exists, and the value in each, at one moment. It does not show how the state got there, and it does not show what happens next.

That limitation is exactly what makes it useful for debugging. A bug is a moment where the state stops matching what you expected, and finding it means comparing snapshots statement by statement until the first mismatch. A diagram without a 'this is after line 3' label cannot be compared to anything.

35. Printing variables

Section

Section 2.4

36. Printing the value, and printing the name

Concept

You can display the current value of a variable with print or println. When we talk about displaying a variable, we generally mean the value of the variable. To display the name, you have to put it in quotes.

String firstLine = "Hello, again!";
System.out.print("The value of firstLine is ");
System.out.println(firstLine);
what is printedin quotes?what appears
"The value of firstLine is "yesthe words themselves
firstLinenothe value: Hello, again!

The output is: The value of firstLine is Hello, again! — quotes mean literal text, no quotes means look up the variable and print what is inside.

37. Quoted or not: the whole rule

Picture it

One decision, made every time you print. It is the same code-versus-data distinction from Lesson 1b, wearing different clothes.

Figure (svg): Two boxes contrasting printing text in quotes with printing a variable without quotes

Two statements that differ by two characters and produce completely different output. When printed text looks like a variable name, this is always the cause.

38. Printing a time from two variables

Worked example

Conveniently, the code for displaying a variable is the same regardless of its type. Build a line of output from literal text and two int variables.

int hour = 11;
int minute = 59;
System.out.print("The current time is ");
System.out.print(hour);
System.out.print(":");
System.out.print(minute);
System.out.println(".");
statementquoted?output so far
print("The current time is ")yesThe current time is
print(hour)noThe current time is 11
print(":")yesThe current time is 11:
print(minute)noThe current time is 11:59
println(".")yesThe current time is 11:59.

Alternate quoted text and unquoted variables.

Why: Literal punctuation like the colon goes in quotes; values do not.

Use print for everything but the last piece.

Why: Otherwise each piece lands on its own line.

Finish with println.

Why: And do not forget it — see the warning below.

Verify: Run it and expect exactly one line: The current time is 11:59.

Why: If nothing appeared at all, you left off the final println — which is a real hazard, not a hypothetical one.

39. What does this display?

Prediction

Watch the quotation marks.

int score = 87;
System.out.print("score");
System.out.print(" = ");
System.out.println(score);
argumentquoted?output
"score"yesscore
" = "yes =
scoreno87

Predict first

What appears on the screen?

  • score = 87
  • 87 = 87
  • score = score
  • score87

Correct: score = 87

Why: The first argument is quoted so the word 'score' is printed literally; the third is unquoted so Java looks up the variable and prints 87. This pattern — a quoted label followed by an unquoted value — is how nearly every piece of labelled output in this book is written.

40. Do not forget the final println

Concept

Think Java gives an unusually specific warning here, and it is worth taking seriously because the symptom is so confusing.

Figure (svg): A rule card warning that output from print may be buffered and only displayed when println runs

what you wrotewhat you might see
several prints, ending with printlnthe line, as expected
several prints, no printlnnothing — until something else ends the line
several prints, no println, program endspossibly nothing at all

On many computers the output from print is stored without being displayed until println runs; then the entire line is displayed at once. If you omit the println, the program might display the stored output at unexpected times, or even terminate without displaying anything.

41. Printing the name when you meant the value

Trap

The trap

Quotes around everything, on the theory that print needs them.

int hour = 11;
System.out.print("The hour is ");
System.out.println("hour");
statementread asoutput
print("The hour is ")literal textThe hour is
println("hour")literal texthour

Output: The hour is hour. No error, no warning — just the wrong thing, every time.

The fix

Quotes only around the literal text.

int hour = 11;
System.out.print("The hour is ");
System.out.println(hour);
statementread asoutput
print("The hour is ")literal textThe hour is
println(hour)the variable — look up its value11

Output: The hour is 11. The rule is unchanged from Lesson 1b — inside quotes is data, outside quotes is code — and here 'code' means 'a variable to look up'.

42. Label a value

Fill the middle

Produce the output: minutes: 59

Fill in the blanks

int minute = 59;
System.out.print("minutes: ");
System.out.println(minute);

Why: The label is literal text and needs quotation marks, including the trailing space that separates it from the number. The value is the variable itself with no quotes, so Java looks it up. Quoting the second one would print the word 'minute' instead of 59.

43. Nothing appeared. What happened?

Hypothesis

A student's program runs, exits normally, and displays nothing at all. The code is three print calls with no println.

Predict first

What is the most likely explanation?

  • The output was stored waiting for a newline, and the program ended before it was displayed
  • print does not work and only println does
  • The variables were never initialised
  • The program crashed silently

Correct: The output was stored waiting for a newline, and the program ended before it was displayed

Why: On many computers output from print is buffered — stored without being displayed — until a println flushes the line. A program that only ever calls print can therefore terminate with its output still waiting. This is exactly why Think Java says 'don't forget the println', and why the symptom is no output rather than wrong output.

44. Teach the quoting rule

Explain it

The rule is short; the explanation of why is what makes it stick.

Discussion prompt

A classmate wrote System.out.println("total"); and cannot understand why it printed the word 'total' rather than the number in their total variable. Explain the rule and, more importantly, explain why Java cannot simply work out what they meant.

Hint: What would happen if Java tried to guess?

Answer:

The rule: quotes mean this exact text; no quotes means look up this name and print what is in it.

Java cannot guess, because both are things people genuinely want. Printing the literal word 'total' as a label is just as common as printing the value — in fact the usual line does both: print("total: "); println(total);. If Java tried to be helpful, there would be no way left to print a label that happens to share a name with a variable.

45. Operators, expressions, and integer division

Section

Section 2.5

46. Operators combine values into expressions

Concept

Operators are symbols that represent simple computations: + for addition, - for subtraction, * for multiplication and / for division. An expression is a combination of numbers, variables and operators that represents a single value to be computed.

int hour = 11;
int minute = 59;
System.out.print("Number of minutes since midnight: ");
System.out.println(hour * 60 + minute);
stepthe expression
as writtenhour * 60 + minute
replace each variable with its value11 * 60 + 59
multiplication happens first660 + 59
then the addition719

operator — A symbol that represents a simple computation, such as + or *.

expression — A combination of numbers, variables and operators that computes to a single value.

operand — A value that an operator works with.

When the program runs, each variable is replaced by its current value, and then the operators are applied. That two-step reading is how you evaluate any expression by hand.

47. Evaluating an expression, one step at a time

Picture it

Multiplication before addition, exactly as in arithmetic. The useful habit is to write each intermediate form down rather than doing it all in your head.

Figure (svg): A trace strip reducing the expression hour times 60 plus minute to 719 in three steps

Every expression in this book can be reduced this way. When one surprises you, write the steps out — the surprise is almost always at a single step you can then point at.

48. The division that surprises everyone

Worked example

This fragment tries to compute the fraction of an hour that has passed. It is correct Java, it compiles cleanly, and the answer is wrong.

int minute = 59;
System.out.print("Fraction of the hour that has passed: ");
System.out.println(minute / 60);
stepvaluenote
minute / 6059 / 60both operands are integers
exact answer0.98333...but this is not what Java computes
integer division0rounds toward zero
displayed0correct Java, wrong answer

Notice the types of both operands.

Why: minute is an int and 60 is an int. Both integers.

Java therefore performs integer division.

Why: By design, integer division always rounds toward zero — even when the next integer is very close.

Accept that 59 / 60 is 0.

Why: Not a bug and not a rounding accident. It is the defined behaviour of / on two integers.

Work around it by scaling first.

Why: minute * 100 / 60 multiplies before dividing, so the division has something to work with.

Verify: Change the statement to System.out.println(minute * 100 / 60); and expect 98.

Why: Again the result is rounded down, but at least now it is approximately correct. Section 2.6 gives the general fix — floating-point numbers.

49. What is 7 / 2 in Java?

Prediction

Both operands are integers.

System.out.println(7 / 2);
operandskind of divisionresult
7 and 2, both intinteger division3
exact value—3.5
what happened to the .5discarded — rounds toward zero—

Predict first

What is displayed?

  • 3
  • 3.5
  • 4
  • 3.0

Correct: 3

Why: Both operands are integers, so Java performs integer division and rounds toward zero, discarding the 0.5. Note that it rounds toward zero rather than to the nearest integer — 7/2 is 3, and it would still be 3 if the exact answer were 3.9.

50. Why order matters more than usual here

Concept

minute * 100 / 60 gives 98, but minute / 60 * 100 gives 0. In ordinary arithmetic these are the same quantity. With integer division they are not, because information is discarded at each division.

expressionevaluated left to rightresult
minute * 100 / 6059 * 100 = 5900, then 5900 / 6098
minute / 60 * 10059 / 60 = 0, then 0 * 1000
(the exact value)59 / 60 = 0.98333...98.33...

The rule to take away: with integer division, multiply before you divide. Each division throws away the remainder, so doing one early destroys precision that later steps cannot recover.

51. Expecting 0.98 from 59 / 60

Trap

The trap

The expectation. Division means division, so this should be about 0.98.

int minute = 59;
System.out.println(minute / 60);   // expected 0.98333
operandtype
minuteint
60int
therefore /integer division — rounds toward zero

The output is 0. Nothing warns you, because nothing is wrong as far as Java is concerned — you asked for integer division and you got it.

The fix

The rule. Java performs integer division when both operands are integers, and integer division always rounds toward zero.

int minute = 59;
System.out.println(minute * 100 / 60);   // 98 — scale up first
// or, from Section 2.6:
// double m = 59.0;  System.out.println(m / 60.0);   // 0.9833...
approachoperandsresult
minute / 60int, int0
minute * 100 / 60int, int — but scaled first98
59.0 / 60.0double, double0.9833333333333333

Two fixes, and they solve different problems. Scaling keeps integers and gives you a percentage; switching to double gives you the fraction itself. The next lesson takes up the second one.

52. Order the evaluation steps

Ranking

For the expression hour * 60 + minute with hour = 11 and minute = 59.

Put in order

  1. hour * 60 + minute
  2. 11 * 60 + 59
  3. 660 + 59
  4. 719

Why: Java replaces each variable with its current value first, then applies the operators in precedence order — multiplication before addition. Writing the intermediate forms out is the technique that turns a surprising result into a step you can point at.

53. Which expression gives 98?

Estimation

All four use only integers. Only one gives the percentage.

Predict first

With minute = 59, which expression evaluates to 98?

  • minute * 100 / 60
  • minute / 60 * 100
  • minute / 60
  • 100 / 60 * minute

Correct: minute * 100 / 60

Why: Multiplying before dividing keeps the precision: 59 * 100 is 5900, and 5900 / 60 is 98. Any version that divides first destroys the information — 59 / 60 is 0 and 100 / 60 is 1, so the other three give 0, 0 and 59 respectively. With integer division, multiply before you divide.

54. When does integer division give the exact answer?

Edge cases

You have seen it round down. Find the cases where it does not lose anything.

Discussion prompt

Integer division discards a remainder. For which pairs of integers does it discard nothing, so the result is exactly right? And is 'rounds toward zero' the same as 'rounds down' for every pair you can think of?

Hint: Try a negative numerator for the second half.

Answer:

It is exact whenever the numerator is a multiple of the denominator: 6 / 3 is exactly 2, 100 / 25 is exactly 4. There is no remainder to discard.

And no — 'toward zero' and 'down' differ for negatives. -7 / 2 is -3, not -4: rounding toward zero moves a negative result up. Think Java's phrasing, 'always rounds toward zero', is deliberately precise for exactly this reason.

Chapter 3 introduces the remainder operator %, which hands you the part that integer division threw away — and the two together let you recover the exact answer.

55. Declaration, assignment, expression

Comparison

Three new things this lesson added. Fill the blanks to keep them apart.

Comparison matrix

declarationassignmentexpression
exampleint hour;hour = 11;hour * 60 + minute
what it doescreates a named storage locationupdates the value in onecomputes a single value
has a type?yes — you write itno — the variable already has oneyes — decided by its operands
is a statement?yes, ends with a semicolonyes, ends with a semicolonno — it is part of one

The last row is the one that catches people: an expression is not a statement. hour * 60 on its own line is not a legal program — it computes a value and then does nothing with it.

56. The pattern to carry away

Pattern

Two habits from this lesson do more work than anything else: read = as a command, and evaluate expressions by substitution.

when you seeread it asnot as
int x = 5;make a box called x and put 5 in itx equals 5, forever
b = a;copy what is in a into b, nowlink b to a
hour * 60 + minutesubstitute values, then apply operatorsa formula to admire
59 / 60integer division — rounds toward zero0.98333

57. Check: tracing assignments

Check

Work it out before you click.

int p = 4;
int q = p;
p = p + 1;
q = q + p;
afterpq
int p = 4;4—
int q = p;44
p = p + 1;54
q = q + p;59

Check your understanding

What are the final values of p and q?

  • A. p = 5, q = 9 (correct)
  • B. p = 5, q = 8
  • C. p = 4, q = 8
  • D. p = 5, q = 10

Answer: A

Why: q gets a copy of 4, then p becomes 5, then q becomes its own value (4) plus p's current value (5), which is 9. The key step is that changing p after the copy does not retroactively change q, but a later statement reading p does see the new value.

Why B tempts people
This uses p's old value of 4 in the last line, but by then p has already been reassigned to 5.
Why C tempts people
This misses that p = p + 1; actually changes p — the right side is evaluated first, then stored back.
Why D tempts people
This uses q = 5 rather than 4 in the last line, treating q as though it had followed p when p changed.

58. Check: integer division

Check

Work it out before you click.

Check your understanding

With int total = 7; what does System.out.println(total / 2 * 2); display?

  • A. 6 (correct)
  • B. 7
  • C. 3.5
  • D. 8

Answer: A

Why: Evaluation is left to right for operators of equal precedence: 7 / 2 is 3 by integer division, and 3 * 2 is 6. The remainder discarded by the division is gone for good, so multiplying back by 2 does not recover the original 7 — which is a compact demonstration of why you should multiply before dividing.

Why B tempts people
This assumes dividing and then multiplying by the same number returns the original value, which fails whenever the division discards a remainder.
Why C tempts people
Both operands are integers, so Java never produces a fractional result here — that would need a double.
Why D tempts people
This rounds 3.5 up to 4 before multiplying, but integer division rounds toward zero, giving 3.

59. Check: quoted or not

Check

Work it out before you click.

int n = 5;
System.out.println("n" + " = " + n);
piecequoted?contributes
"n"yesthe letter n
" = "yes =
nnothe value 5

Check your understanding

What does this display?

  • A. n = 5 (correct)
  • B. 5 = 5
  • C. n = n
  • D. n=5

Answer: A

Why: The first two pieces are in quotation marks and print literally, while the third is the bare variable name, so Java looks it up and prints 5. The spaces around the equals sign are inside the quotes, so they appear in the output exactly as typed.

Why B tempts people
This treats the quoted "n" as though it were the variable, but quotation marks always mean literal text.
Why C tempts people
This treats the unquoted n as literal text, which is backwards — without quotes Java looks the name up.
Why D tempts people
The spaces are inside the quoted string " = ", so they are printed; nothing removes them.

60. Integer division in the wild

Real world

This is not a beginner's trap that you outgrow. Integer division causes real bugs in shipped software, and the reason is always the same.

Discussion prompt

Think of somewhere a program divides one whole number by another — splitting a bill, working out a percentage, paging through search results. Where could rounding toward zero cause a visible bug, and who would notice?

Hint: Think about 'showing results 1 to 10 of 95'.

Answer:

Pagination is the classic case: 95 results at 10 per page is 95 / 10, which is 9 — and the last five results become unreachable. The fix is (95 + 9) / 10, and getting it wrong is a bug users hit rather than a bug tests catch.

Splitting money is the other one. Dividing 100 pence three ways gives 33 each and loses a penny, which is why financial code tracks the remainder explicitly rather than pretending it does not exist.

The transferable habit: whenever you write / between two integers, ask what happens to the remainder. Sometimes discarding it is exactly right. The bug is not the rounding — it is not having decided.

61. How sure are you?

Commit first

Commit to an answer and to your confidence in it.

Predict first

After int a = 5; int b = a; a = 3; what is the value of b?

  • 5
  • 3
  • 8
  • b has no value

Correct: 5

Why: b was given a copy of a's value at the moment int b = a; ran, and that value was 5. Reassigning a afterwards changes only a — the two variables are separate boxes, and the assignment created no lasting link between them. If you were confident the answer was 3, you are reading = as mathematical equality, which is the single most useful misconception to have corrected in this chapter.

62. Explain it to someone else

Explain it

Two minutes, out loud, no slides.

Discussion prompt

Explain to someone who knows algebra but not programming why a = a + 1; is a perfectly sensible statement in Java, even though it is nonsense as an equation. Then explain why 7 = a; is not allowed.

Hint: The word to reach for is 'command'.

Answer:

In algebra a = a + 1 has no solution. In Java it is not a claim at all — it is an instruction: take what is currently in a, add one to it, and put the result back in a. Read right to left, it is completely reasonable.

And 7 = a; is not allowed because the left side names where to put the value. a is a box; 7 is not a box, so there is nowhere for the value to go.

If you found yourself saying 'equals' out loud, try 'gets' or 'becomes' instead — a gets a + 1. Changing the word you use is the fastest way to change the reading.

63. Exit ticket

Exit ticket

One question before you close the deck.

Predict first

Why does System.out.println(59 / 60); display 0 rather than 0.98333?

  • Both operands are integers, so Java performs integer division, which rounds toward zero
  • Java cannot represent decimal numbers
  • println can only display whole numbers
  • 59 / 60 really is 0

Correct: Both operands are integers, so Java performs integer division, which rounds toward zero

Why: The types of the operands decide which division is performed. Two ints mean integer division, and integer division discards the remainder rather than rounding to the nearest value. Java can represent 0.98333 perfectly well — that is what double is for, and it is the subject of the next lesson — but nothing in this expression asked for it.

64. Draw the whole lesson

Connect it up

One page, from memory.

Draw it

Draw the memory diagram after each of these four statements, four diagrams in a row: int a = 5; int b = a; a = 3; int c = a * 60 + b;. Label each diagram with the statement it follows. Then, underneath, write the value of b / a and the value of b * 100 / a, and one sentence saying why they are not simply 100 times apart.

65. Recap

Recap

Five sections that between them turn a program from something that prints fixed text into something that can hold a value and compute with it.

if you remember one thingit is this
about assignmentread = as 'gets', never as 'equals'
about tracingone column per variable, one row per statement
about arithmetictwo ints divided give an int

Sources

  1. Downey & Mayfield, Think Java, 2nd edition (Green Tea Press / O'Reilly, 2020) — Think Java 2e, Chapter 2 (Variables and Operators), Sections 2.1-2.5, pp. 17-23
  2. The Java Tutorials — Variables
  3. Think Java 2e — free online edition and source code

Want this taught 1-on-1? Alexander tutors Java — $55/session, free consultation.

Book on Wyzant · Text (657) 465-8108