Card 09/ 09

RecapDifficulty: Intermediate1 min

What the Variable Holds, and What the Type Guarantees

Five things this topic established, and where each one was.

What the topic established, and which card it was on
The thingWhere
A primitive variable holds the value; a reference variable holds an addressWhat a Variable Holds, and Why That Is Two Answers
Assignment copies what the variable holds, which is why two names can reach one listTwo Names for One Shopping Basket
Eight primitive types, each with a size and a default for fieldsThe Eight Types Everything Else Is Built From
Money is never a double; it is pence in a long or a BigDecimalint, long, double or BigDecimal
Widening is automatic, narrowing needs a cast, and the cast is you taking responsibilityWhy the Compiler Refuses an Assignment You Have Not Run Yet

The one worth carrying: the declared type is a promise checked before anything runs, and what the variable holds decides what assignment does. Those two facts between them answer most questions about why a value changed when you did not expect it to.