Skip to content

Variables & Types

The Course
•
20 min study

A variable gives a value a name. A declaration says what kind of value the name can hold: int minutes = 25; creates an integer variable and initializes it. String holds text; boolean holds true or false. Java checks these types before running. The spelling and capitalization matter: String begins with a capital S.

Choose names that explain the value, such as plannedMinutes rather than x. Names are case-sensitive: minutes and Minutes are different. A declaration introduces a name once in its block; later assignment uses the existing name without repeating its type. Read = as "store the value on the right in the variable on the left", rather than as a mathematics claim that two expressions are always equal.

Follow the current value

Evaluate the right-hand side using the values that exist at that moment, then store the result. A later assignment does not go back and update earlier copies. Trace a little table with a row after every assignment. Predict the printed values from the final row, not from the first declaration.

Name, update, then report Java
public class Main {
    public static void main(String[] args) {
        String topic = "Java";
        int minutes = 20;
        boolean finished = false;

        minutes = minutes + 5;
        finished = true;

        System.out.println(topic);
        System.out.println(minutes);
        System.out.println(finished);
    }
}
Check your prediction
Text
Java
25
true

The integer update reads 20 and stores 25. The boolean assignment changes a fact about this run; it does not print anything by itself. Put a print before the update to observe both old and new values.

An earlier value is a copy Java
public class Main {
    public static void main(String[] args) {
        int planned = 20;
        int saved = planned;

        planned = 30;

        System.out.println(planned);
        System.out.println(saved);
    }
}
Check your prediction
Text
30
20

For these integer values, saved receives a copy of the number 20. It is not a live formula linked to planned. Reverse the assignment order and trace again. We will distinguish this from object references later.

Compiler error: a value of the wrong type Java
public class Main {
    public static void main(String[] args) {
        int minutes = "25";
        System.out.println(minutes);
    }
}
Read the diagnostic

The quoted value is a String, not an int. A number written as text is useful for messages but is not an integer value. Remove the quotes to initialize an int. Converting numeric text into a number is taught on Day 6.

Trace assignments as a sequence

A variable is not a spreadsheet formula that stays connected to every value used to calculate it. If you compute remaining from budget, then later change budget, remaining keeps its previously computed value until you assign it again. Read an assignment as “evaluate the right side now, then store that result on the left.”

Write one row per statement and the values after it runs:

Statementbudgetremaining
int budget = 60;60(none yet)
int remaining = budget - 25;6035
budget = 90;9035
remaining = budget - 25;9065

Row 3 is the point: changing budget leaves remaining at 35. Only row 4, a new calculation, makes it 65. Type these four lines into the editor above, print remaining after row 3 and again after row 4, and check both values against the table.

Your turn

Exercise

Track a study budget

Start with 60 available minutes. Reserve 25 for Java, then 10 for reading. Store the updated remaining value after each reservation and print 25. Print the original budget separately as 60. Do not hard-code the final remaining total.

Your workspace Java
public class Main {
    public static void main(String[] args) {
        int budget = 60;
        // Copy the budget, then subtract each reservation.
    }
}
Hint 1: the idea

Keep the original budget and the changing remainder in different variables.

Hint 2: a first step

Initialize remaining from budget. The first update subtracts the Java session from remaining.

Show one solution
Java
public class Main {
    public static void main(String[] args) {
        int budget = 60;
        int remaining = budget;

        remaining = remaining - 25;
        remaining = remaining - 10;

        System.out.println(remaining);
        System.out.println(budget);
    }
}
Text
25
60

The key line is int remaining = budget;. Each subtraction updates the copy, so budget still holds 60 when it is printed.

Change it and explain

Try a budget of 35, then 20. A negative result is possible arithmetic; it does not automatically stop your program. Later you will add a decision to reject an impossible plan. For now, explain what each variable means and why copying the budget protects the original number.

Use final int dailyGoal = 30; when a variable should not be assigned another value after initialization. Try changing dailyGoal afterward and read the error. Final is a rule about assignment, not a promise that all future objects are immutable. Use ordinary variables for values you intend to update.

Was this article helpful?

Your response is saved on this device.