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.
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
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.
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
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.
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.
Your turn
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.
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
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);
}
}25
60 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.
Completion is saved in this browser only. It does not measure mastery.