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 does

What 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 typeWhat it holds
A whole numberA whole value, with no fraction
A decimal numberA value with a fraction
A booleanTrue or false only
A stringA sequence of characters

Example 1 — why the error is stopped early

  1. The value does not fit the type.
  2. The check refuses at the line itself.
  3. And not in the sixth function.

Example 2 — why a type is not a comment

  1. A comment can go stale in silence.
  2. A type is enforced at every line.
  3. So it cannot lie.
The refused line is exactly where the bug is born — not the sixth function where it would have blown up.