Card 02/ 09
All 9 cards
ExampleDifficulty: Intermediate1 min
One Method, Two Regions
Four lines, and each one puts something in a different place.
void process() {
int count = 3;
String label = "total";
List<String> rows = new ArrayList<>();
rows.add(label);
}| Thing | Lives | Gone when |
|---|---|---|
count, holding 3 | The stack frame | process() returns |
label, holding an address | The stack frame | process() returns |
The string "total" | The heap, in the pooled area | Effectively never, while the class is loaded |
rows, holding an address | The stack frame | process() returns |
The ArrayList object | The heap | Nothing can reach it any more |
Three of the five are variables, and all three vanish together when the frame is discarded. Neither of the two objects vanishes at that moment — they become unreachable, which is a different thing and happens first.
What to notice: nothing in the method decided when the list would be freed. The frame going away only removed the last thing pointing at it.