Grade 11 Computer Science · The bridge to Java
A declared type, and what it buys
Why it is worth writing what each name holds.
A string that rode through five functions
A bug that blows up in the sixth function was sometimes born five functions earlier, and no message will tell you where. One word written beside a variable’s name can stop it at the line where it was born — but only if someone is committed to checking it.
The declaration looks like superfluous work: you wrote a name, and now you must write beside it what it holds too. The gain is not immediate, and it becomes clear the moment the program is large enough that you do not remember what you put in each variable.
int lessons = 7;
double average = 84.5;
boolean passed = true;
String name = "Dana";
// lessons = "seven"; // refused before the program runs
lessons = lessons + 1; // fine — that is what a whole number doesWhat a declaration actually prevents
The category that disappears is that of a value of the wrong kind that travelled far. In Python, a string that entered a variable meant to hold a number will pass through five functions and explode in the sixth, and the message there will not say where it came from. In Java it is stopped at the line where it was written. This is exactly the boundary-check principle you learned — only here the language enforces it for you.
| The type | What it holds |
|---|---|
| A whole number | A whole value, with no fraction |
| A decimal number | A value with a fraction |
| A boolean | True or false only |
| A string | A sequence of characters |
Example 1 — why the error is stopped early
- The value does not fit the type.
- The check refuses at the line itself.
- And not in the sixth function.
Example 2 — why a type is not a comment
- A comment can go stale in silence.
- A type is enforced at every line.
- So it cannot lie.