2.1 Variables and Types

Java Variables and Types for Beginners

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

A game has to remember things. How many lives are left, whether the player has the key, what the score is. A variable is how a program remembers, and unlike your memory it never gets it slightly wrong.

What you will be able to do

  • Declare a variable with the right type for what it holds
  • Tell the difference between declaring a variable and assigning to it
  • Choose between int, double, boolean and String for a given job

Words you will need

Variable
In plain words A named box that holds one value.
More precisely A named storage location with a declared type.
Type
In plain words What kind of thing the box is allowed to hold.
More precisely The set of values a variable may take and the operations allowed on it.
Declare
In plain words Make the box, and say what goes in it.
More precisely Introduce a variable with a type and a name: `int score;`
Assign
In plain words Put a value in the box.
More precisely Store a value in an existing variable with `=`.

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 A Greenfoot actor class with a single int variable declared and assigned inside a method.

    int score = 0;

    Three things in one line, and it is worth separating them.

    `int` is the TYPE: this box holds whole numbers. `score` is the NAME you will use from now on. `= 0` puts a starting value in.

    You could split it: `int score;` then `score = 0;`. Same result. Beginners are often shown only the combined form and then cannot read the split one.

  2. Step 2

    On your screen Four variable declarations of different types shown together in an editor.

    int    lives    = 3;
    double speed    = 2.5;
    boolean hasKey  = false;
    String  name    = "Crab";

    The four types you need for the whole of this course.

    `int` counts things. Lives, score, coins. `double` measures things where fractions matter. Speed, distance. `boolean` is a yes or no. It can only ever be true or false. `String` is text. The double quotes are part of the value, not part of the word.

  3. Step 3

    On your screen The same variable being assigned a new value on a later line.

    int score = 0;
    score = 10;
    score = 25;

    The type is written ONCE, when the box is made. After that you just use the name.

    Writing `int score = 10;` on the second line is an error: you are trying to make a second box with a name that is already taken.

    Each assignment replaces what was there. After these three lines score is 25, and the 0 and the 10 are gone.

  4. Step 4

    On your screen An actor using a variable to move by a stored speed value.

    int stepSize = 4;
    move(stepSize);

    And this is why it matters. `move(stepSize)` reads the box and uses whatever is in it. Change the 4 in one place and every move in the class changes with it.

    That is the real argument for variables, and it is not about memory. It is that a number written in one place can be changed in one place.

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 You have to write the type every time you use a variable.

What is actually true The type is written once, at the declaration. After that, just the name.

Why it matters Re-declaring produces "variable is already defined", which reads as nonsense if you think the type is part of the name.

Common wrong idea A String and a number are interchangeable if they look the same.

What is actually true "5" is text and 5 is a number. They behave completely differently.

Why it matters This is where `"5" + 1` giving `51` instead of `6` comes from.

Common wrong idea boolean can hold yes, no, 1 or 0.

What is actually true A boolean is exactly true or false. Nothing else.

Why it matters Java will refuse `boolean done = 1;`, and the error is confusing if you expect 1 to mean true the way it does in some other languages.

Common wrong idea Assigning a new value adds to the old one.

What is actually true Assigning REPLACES. `score = 10;` throws away whatever was there.

Why it matters Adding is `score = score + 10;`, which looks like nonsense as math and is the next lesson.

Check your understanding

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

  1. 1. Which type should hold the number of lives a player has left?

  2. 2. Which type should hold whether the player has picked up the key?

  3. 3. What is wrong with this pair of lines? int score = 0; int score = 10;

  4. 4. After these lines, what is in lives? int lives = 3; lives = 5; lives = 1;

  5. 5. Which declaration is written correctly?

Fill in the code

An actor needs to remember three things: how many coins it has collected (a whole number starting at zero), how fast it moves (2.5), and whether the game is over (not yet). Fill in the types and the starting values.

 coins = ;
double speed = 2.5;
 gameOver = ;
Stuck? Open a hint for each blank
  • Blank 1: Coins are counted. Which type counts?
  • Blank 2: It has not collected any yet.
  • Blank 3: This one is a yes or no.
  • Blank 4: The game has not ended yet. Which of the two values is that?

Lesson quiz

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

  1. 1. Which of these can a boolean hold?

  2. 2. You need to store a player name. Which type?

  3. 3. What does the `=` do in `int score = 0;`?

  4. 4. Which line correctly changes an EXISTING variable called lives to 2?

The short version

  • A variable is a named box with a type.
  • Declare once with the type; assign as often as you like with just the name.
  • int counts, double measures, boolean is true or false, String is text.
  • Assigning REPLACES the old value.

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]