Unit 02 · lesson
A Variable Has a Type, Not Just a Name
A variable is a named storage location in the program's model. In Java, its declared type limits the values and operations that make sense for that variable.
A Java variable combines type constraints with current state
The declared type limits valid values and operations while assignments change program state.
- DECLARATIONname the variable and declare its typeconstrain
- TYPE RULEdefines valid values and operationsstore
- VALUEcurrent program statereassign
- NEW VALUElater assignments must satisfy the same typeobserve
- BEHAVIORoperations reflect representation and state
void main() {
int attempts = 3;
double temperature = 21.5;
boolean ready = true;
char grade = 'A';
String team = "Robotnix";
IO.println(attempts);
IO.println(temperature);
IO.println(ready);
IO.println(grade);
IO.println(team);
}
String is not a primitive type, but it belongs in the comparison because text appears everywhere in real programs.
Type is a constraint you can reason about
Try to assign text to an integer:
int attempts = "three";
Java rejects it before runtime. That is useful. The declaration establishes an invariant:
attempts must hold an int value
The compiler can now inspect later operations using that rule.
Declaration, initialization, assignment
These words describe different events.
int score; // declaration
score = 10; // assignment
This combines both:
int score = 10; // declaration + initialization
Later:
score = 12; // assignment of a new value
Trace each state rather than calling every line "setting a variable."
var does not make Java dynamically typed
Java can infer a local variable type from its initializer:
var score = 10;
var name = "Maya";
The inferred types are still fixed: score is an int and name is a String.
This will not work:
var score = 10;
score = "ten";
var saves repetition when the type is already clear. It does not mean "this variable can become anything later."
Widening versus narrowing
Java can safely widen some numeric values:
int count = 12;
double measurement = count;
12 can be represented as 12.0 in a double.
The reverse direction may lose information:
double measurement = 12.9;
int count = (int) measurement;
The cast does not round. It discards the fractional portion, producing 12.
A cast is not proof that the conversion is appropriate. It says the programmer is explicitly requesting the conversion.
Design a small data model
Imagine a program tracking one autonomous-robot test.
Choose types for:
- test number;
- elapsed seconds including fractions;
- whether the run completed;
- operator initial;
- short status message.
Write declarations and one sentence for each type choice.
Then challenge one choice. Could testNumber ever contain a leading zero that must be preserved? If the answer is yes, perhaps it is an identifier rather than a quantity and String may represent it better.
That distinction matters. Types should model meaning, not just whatever syntax is easiest to type.
Evidence
Create a table with five values from a fictional application. For each, provide:
- semantic meaning;
- Java type;
- example value;
- one invalid value or operation that the type helps reject;
- whether
varwould improve or reduce readability in that declaration.
The goal is to justify representation, not memorize a primitive-type chart.