Java has the provision of "primitive" data types. They are called primitives because they do not have any behavior associated with them. They are just raw data. If you want to run a loop 10 times, all you need is 9.

The counter does not serve any purpose after the loop is over and so, you do not need any permanent storage to store the counter value. A simple int variable that is forgotten as soon as the loop is done, is sufficient for this purpose.

Stack space is perfect for keeping such values because a stack space is wiped clean as soon as the thread that owns that stack space is finished. There is no need of any garbage collection to happen here.


Furthermore, a program can directly manipulate the value of a primitive variable without having to go through the indirection of a reference.

← Back to Learning Journey