7.8. Stack and Heap Memory

In this section we will look at how variables are stored in memory in Java. We are examining memory in Java at this point so that you can understand at a lower level what happens when you create and manipulate the objects that make up your programs.

Primitive data types have just one value to store. For instance:

int i = 1; 

The appropriate amount of space is allocated given the data type, and the variable is stored in memory just as it is.

Objects must be stored differently because they are more complex. They often hold multiple values, each of which must be stored in memory. The association between each value and the object must be maintained throughout its life. An object reference variable must then hold a reference ...

Get Java™ for ColdFusion® Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.