Topics in this unit
1
Variables and Types
Practice this topic →Know
A variable is a named box holding one value, and in Java you declare what kind of value it holds. A whole number and a decimal number are different kinds and behave differently.
Apply
Declare the type that matches the data: whole counts as int, measurements as double, yes-or-no as boolean.
Watch out
Storing a measurement as a whole number, which quietly throws away the decimal part.
2
Whole-Number Division
Practice this topic →Know
When two whole numbers are divided, Java gives a whole number back and throws away the remainder. It does not round; it cuts.
Apply
Use division for how many fit and the remainder operator for what is left over.
Watch out
Expecting 7 divided by 2 to be 3.5. Between two whole numbers it is 3, and this surprises nearly everyone once.
3
Changing Types
Practice this topic →Know
A cast tells Java to treat a value as a different type. Where you put it changes the answer, because it applies to what comes immediately after.
Apply
Do the arithmetic first and cast afterwards when you want the decimal work to happen.
Watch out
Casting too early, which throws away the decimals before they were used.
4
Joining Text and Numbers
Practice this topic →Know
The plus sign adds numbers but joins text. Once text appears, everything after it is joined rather than added.
Apply
Put brackets around arithmetic that must happen before the joining.
Watch out
Getting two digits stuck together instead of their sum, because text came first in the line.
Emphasized in this unit
Connections and techniques that receive extra attention in this unit.
- • Choosing a type that matches the real data
- • Treating whole-number division as a rule rather than a bug
- • Predicting output before running
Varies by course
Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.
- Console input. Some courses read from the keyboard from day one; others supply values in code.
- char type. Introduced early in some sections and with Strings in others.
Check yourself
- • Why does Java want to know the type of every variable?
- • What happens to the remainder when two whole numbers are divided?
- • Why can a plus sign behave in two different ways?
Modeling drill
Write a program that turns a number of seconds into minutes and leftover seconds, then check it on 0, 59, and 61.
VariableTypeintdoubleWhole-number divisionRemainderCast
Need help with this topic?
Work through questions with a tutor and get help with the steps you find difficult.
Get help with this topic