Skip to main content
← Intro to CS (Java) Study Guide

Unit 1 · Take your time

First Programs and Values

Storing values and doing arithmetic with them. Java asks you to say what kind of value each variable holds, which feels like extra work for about a week and then starts catching your mistakes for you.

What a strong answer looks like

A strong Unit 1 answer says what kind of value each variable holds before saying what the arithmetic produces.

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.

Study move

Declare one variable of each basic type and print all of them.

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.

Study move

Work out how many full hours and leftover minutes are in 500 minutes.

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.

Study move

Write the same calculation with the cast in two places and compare the results.

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.

Study move

Print a sentence containing a sum, with and without brackets, and explain the difference.

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.

Mastery checklist

  • Declare a variable of each basic type.
  • Predict the result of dividing two whole numbers.
  • Place a cast so the decimals survive.
  • Print a sentence that includes a correct sum.

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