Card 09/ 09
Recap: Variables and Types
What a variable holds, what assignment copies, the eight primitives, money and casts, with where each was.
Variables and types come down to the six ideas below, each listed with the card that covers it.
| The idea | Where |
|---|---|
| A primitive variable holds the value; a reference variable holds an address | Primitive and Reference Variables: What a Variable Holds |
| Assignment copies what the variable holds, which is why two names can reach one list | Reference Assignment Example: Two Names for One Shopping Basket |
| Eight primitive types, each with a size and a default for fields | Java's Eight Primitive Types |
Money is never a double; it is pence in a long or a BigDecimal | int vs long vs double vs BigDecimal for Money |
| Widening is automatic, narrowing needs a cast, and the cast is you taking responsibility | Static Typing: Widening, Narrowing and Casts |
| Binary fractions cannot hold 0.1 exactly, so decimal sums drift | Floating-Point Error: Why 0.1 + 0.2 Is Not 0.3 |
The declared type is a promise checked before anything runs, and what the variable holds decides what assignment does. Between them, those two facts answer most questions about why a value changed when you did not expect it to.
← → move through sections and cards