Regardless of its type, there are three fundamental operations throughout the lifetime of a variable:
- Initialization: The start of its life
- Assignment: Changing its value as a whole
- Finalization: The end of its life
To be considered a variable, it must first be initialized. There may be final operations for some types. The value of a variable may change during its lifetime.
Every variable must be initialized before being used. Initialization involves two steps:
- Reserving space for the variable: This space is where the value of the variable is stored in memory.
- Construction: This step involves setting the first value of the variable on that space (or the first values of the members of structs and classes).
Every variable lives in a place in memory that is reserved for it. Some of the code that the compiler generates is about reserving space for each variable. Let’s consider the following variable:
int speed = 123;
As we have seen in the value types and reference types chapter, we can imagine this variable living on some part of the memory: