Free degree-level programming lessons for careful independent study.

Degree Level Programmes · Programming 1 · Lesson 4

Declaring and Assigning Variables

Understand declaration, assignment and reassignment as controlled changes to program state.

Lesson Overview

Understand declaration, assignment and reassignment as controlled changes to program state.

Portfolio focus: Trace the value of one variable through five assignments.

ConceptState manipulation
Run fileDeclaringAssigningVariablesDemo.java
Baseline12
Evidence3 tasks

Starter: think before typing

Before running this state manipulation example, find the line where the main idea becomes active. Write a prediction: what must already be true for that line to work, and what should be different after it runs? The checked run ends with `12`; predict how the focus line helps produce that evidence.

Learning Objectives

  • Declare a variable with a type and name.
  • Assign an initial value.
  • Reassign a value deliberately.
  • Explain the difference between the variable and the value it currently stores.

Learning Outcomes

  • By the end of the lesson, you can declare a variable with a type and name.
  • By the end of the lesson, you can assign an initial value.
  • By the end of the lesson, you can reassign a value deliberately.
  • By the end of the lesson, you can explain the difference between the variable and the value it currently stores.

Why this idea exists

Variables exist because programs need named places for changing information: marks, totals, choices, counters, filenames, states and many other values that matter to the problem.

Declaration separates the idea of a name from the value currently stored there. Assignment then changes that stored value, which is why tracing assignments is one of the first steps toward understanding program state.

This topic fits the programming arc as the first serious model of memory and change. Later lessons on loops, arrays, objects and files all depend on knowing what data exists, where it is stored and when it is updated.

Deep dive

Mechanism in this example

The important mechanism is visible around `int score = 10;`. Read it as a concrete move in the program, not as decorative syntax: identify what value, object, branch, call or boundary is being created at that point.

Design pressure

This topic fits the programming arc as the first serious model of memory and change. Later lessons on loops, arrays, objects and files all depend on knowing what data exists, where it is stored and when it is updated.

Failure mode to watch

For Declaring and Assigning Variables, deliberately disturb the assumption behind `int score = 10;`: use an awkward value, missing input, wrong order of calls or boundary case. The useful question is how that disturbance exposes a weakness in the state manipulation design.

Extension step

Extend the example by doing this: Explain why `score = score + 1` is not algebra. The point is to make one small change that forces you to revisit the concept, rather than adding unrelated features.

Portfolio standard

The portfolio note should not repeat the lesson wording. It should show the edited code, the run result, and your own explanation of this evidence: write a tiny program that updates a counter.

Lesson visual

A whiteboard showing a labelled memory box named score, first empty, then assigned 10, then reassigned 12.
A whiteboard showing a labelled memory box named score, first empty, then assigned 10, then reassigned 12.Download visual

Type this and run it

Create DeclaringAssigningVariablesDemo.java, type the program, and run it before changing anything. This section is about reproducing the checked baseline.

public class DeclaringAssigningVariablesDemo {
  public static void main(String[] args) {
    int score = 10;
    score = score + 2;
    System.out.println(score);
  }
}

Build and run it with:

javac DeclaringAssigningVariablesDemo.java && java DeclaringAssigningVariablesDemo

Expected baseline: 12

Run the code in your browser

Use the editor as an experiment surface. First run the checked version, then make one small change to the part of the program that demonstrates state manipulation and compare the new behaviour with the reference output.

Line-by-line explanation

Read the code as a sequence of responsibilities. The focus line for this lesson is int score = 10;; the surrounding lines prepare it, use its result or make the behaviour observable.

public class DeclaringAssigningVariablesDemo {

This names the runnable class for the Declaring and Assigning Variables example, giving the compiler and JVM one clear unit to build.

public static void main(String[] args) {

This is the program entry point. In Declaring and Assigning Variables, it keeps the demonstration of state manipulation in one traceable starting script.

int score = 10;

This introduces score as named state for Declaring and Assigning Variables. Later lines can read, update, pass or print that specific value as evidence.

score = score + 2;

This assignment changes score in Declaring and Assigning Variables to score + 2. Trace where that new value is used next.

System.out.println(score);

This prints score as the observable evidence for Declaring and Assigning Variables. The output lets the learner check whether the state manipulation idea behaved as predicted.

}

This closes the innermost Declaring and Assigning Variables block, so the immediately preceding method, branch or loop has finished.

}

This closes the outer Declaring and Assigning Variables structure, returning the reader to the surrounding class or file.

Worked example

From code to explanation

Problem: Use Declaring and Assigning Variables to complete a small portfolio-quality step: Trace the value of one variable through five assignments.

Method: Locate the line `int score = 10;`, explain the exact role it plays, then decide what you would change to extend the example without changing the whole program.

Reveal worked answer

The checked run should produce `12`. A strong answer links the result back to state manipulation: what was created, selected, stored, called or protected, and why that matters for the portfolio task.

Trace the program

Before: Before the key operation, identify the relevant value, object, branch or resource that the state manipulation concept depends on.

During: Trace `int score = 10;` as the Declaring and Assigning Variables example executes. Say whether that operation creates data, checks a condition, calls behaviour, stores information or crosses a boundary.

After: Compare the run with the expected evidence: `12`.

Change: Now explain why `score = score + 1` is not algebra, run again, and explain the smallest reason the behaviour changed.

Common misconception

A common mistake in declaring and assigning variables is treating the example as a finished answer. For state manipulation, the important question is narrower: which operation carries the idea, what does it make possible, and what would break if you changed it carelessly?

Quick checks

1. In this Declaring and Assigning Variables example, what is the best reason to focus on `int score = 10;`?

2. Which evidence is strongest after you edit and rerun this example?

How to study this lesson

For Declaring and Assigning Variables, predict how state manipulation changes the run before you press Run.

Use the first portfolio task as your main edit: Trace the value of one variable through five assignments.

Use the second task as your variation: Explain why `score = score + 1` is not algebra.

Finish with evidence, not a diary entry: Write a tiny program that updates a counter.

Portfolio Practice

  1. Trace the value of one variable through five assignments.
  2. Explain why `score = score + 1` is not algebra.
  3. Write a tiny program that updates a counter.

Final self-check

Can you explain the key operation?

Explain the line identified in the quick check in one or two sentences. Your answer should say what it does before the output 12 appears.

Can you justify the portfolio evidence?

Your evidence should include the original run, one edited run, and a short note explaining how the edit affected state manipulation.

Study route

Practise state manipulation by predicting the Java example, typing it, running it in the browser, tracing the result and saving portfolio evidence.

Next, move into The Main Method and carry forward one improvement from this lesson into the next program.