Lesson Overview
Use Java primitive types, strings, variables, constants and expressions accurately.
Portfolio focus: Write declarations for a student record.
Starter: think before typing
Before running this programming foundations 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 `Programming Portfolio: 30 credits, passed = true`; predict how the focus line helps produce that evidence.
Learning Objectives
- Declare and initialise Java variables.
- Choose between int, double, boolean, char and String.
- Use arithmetic, comparison and logical expressions.
- Explain common type and precision mistakes.
Learning Outcomes
- By the end of the lesson, you can declare and initialise Java variables.
- By the end of the lesson, you can choose between int, double, boolean, char and String.
- By the end of the lesson, you can use arithmetic, comparison and logical expressions.
- By the end of the lesson, you can explain common type and precision mistakes.
Why this idea exists
Types are one of programming's oldest answers to a practical problem: computers store bits, but programmers think in numbers, text, truth values and domain concepts. A type gives meaning and rules to stored data.
Java uses static typing, which means many type mistakes are caught before the program runs. This design choice reflects a trade-off: more declarations in exchange for clearer contracts and earlier error detection.
Understanding types also means understanding representation. `int`, `double`, `boolean` and `String` do not just differ in spelling; they differ in storage, operations, precision and the kinds of bugs they make possible.
Deep dive
Mechanism in this example
The important mechanism is visible around `int credits = 30;`. 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
Understanding types also means understanding representation. `int`, `double`, `boolean` and `String` do not just differ in spelling; they differ in storage, operations, precision and the kinds of bugs they make possible.
Failure mode to watch
For Java Types, Variables and Expressions, deliberately disturb the assumption behind `int credits = 30;`: 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 programming foundations design.
Extension step
Extend the example by doing this: Calculate a weighted mark. 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: explain why integer division can surprise new java programmers.
Lesson visual

Type this and run it
Create JavaTypesVariablesExpressionsDemo.java, type the program, and run it before changing anything. This section is about reproducing the checked baseline.
public class JavaTypesVariablesExpressionsDemo {
public static void main(String[] args) {
int credits = 30;
double average = 67.5;
boolean passed = average >= 40.0;
String module = "Programming Portfolio";
System.out.println(module + ": " + credits + " credits, passed = " + passed);
}
}Build and run it with:
javac JavaTypesVariablesExpressionsDemo.java && java JavaTypesVariablesExpressionsDemoExpected baseline: Programming Portfolio: 30 credits, passed = true
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 programming foundations and compare the new behaviour with the reference output.
Programming Portfolio: 30 credits, passed = trueLine-by-line explanation
Read the code as a sequence of responsibilities. The focus line for this lesson is int credits = 30;; the surrounding lines prepare it, use its result or make the behaviour observable.
public class JavaTypesVariablesExpressionsDemo {This names the runnable class for the Java Types, Variables and Expressions example, giving the compiler and JVM one clear unit to build.
public static void main(String[] args) {This is the program entry point. In Java Types, Variables and Expressions, it keeps the demonstration of programming foundations in one traceable starting script.
int credits = 30;This introduces credits as named state for Java Types, Variables and Expressions. Later lines can read, update, pass or print that specific value as evidence.
double average = 67.5;This introduces average as named state for Java Types, Variables and Expressions. Later lines can read, update, pass or print that specific value as evidence.
boolean passed = average >= 40.0;This introduces passed as named state for Java Types, Variables and Expressions. Later lines can read, update, pass or print that specific value as evidence.
String module = "Programming Portfolio";This introduces module as named state for Java Types, Variables and Expressions. Later lines can read, update, pass or print that specific value as evidence.
System.out.println(module + ": " + credits + " credits, passed = " + passed);This prints module + ": " + credits + " credits, passed = " + passed as the observable evidence for Java Types, Variables and Expressions. The output lets the learner check whether the programming foundations idea behaved as predicted.
}This closes the innermost Java Types, Variables and Expressions block, so the immediately preceding method, branch or loop has finished.
}This closes the outer Java Types, Variables and Expressions structure, returning the reader to the surrounding class or file.
Worked example
From code to explanation
Problem: Use Java Types, Variables and Expressions to complete a small portfolio-quality step: Write declarations for a student record.
Method: Locate the line `int credits = 30;`, 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 `Programming Portfolio: 30 credits, passed = true`. A strong answer links the result back to programming foundations: 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 programming foundations concept depends on.
During: Trace `int credits = 30;` as the Java Types, Variables and Expressions 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: `Programming Portfolio: 30 credits, passed = true`.
Change: Now calculate a weighted mark, run again, and explain the smallest reason the behaviour changed.
Common misconception
A common mistake in java types, variables and expressions is treating the example as a finished answer. For programming foundations, 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 Java Types, Variables and Expressions example, what is the best reason to focus on `int credits = 30;`?
2. Which evidence is strongest after you edit and rerun this example?
How to study this lesson
For Java Types, Variables and Expressions, predict how programming foundations changes the run before you press Run.
Use the first portfolio task as your main edit: Write declarations for a student record.
Use the second task as your variation: Calculate a weighted mark.
Finish with evidence, not a diary entry: Explain why integer division can surprise new Java programmers.
Portfolio Practice
- Write declarations for a student record.
- Calculate a weighted mark.
- Explain why integer division can surprise new Java programmers.
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 Programming Portfolio: 30 credits, passed = true 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 programming foundations.
Study route
Practise programming foundations by predicting the Java example, typing it, running it in the browser, tracing the result and saving portfolio evidence.
Next, move into Selection and Iteration in Java and carry forward one improvement from this lesson into the next program.
