Stack and Heap Diagram — 4. Arrays

Stack and Heap Diagram is a sequence consisting of the following 4 notional machines:
1. Objects
2. Stack Frames
3. References
4. Arrays

Programming Language

imperative, OO / Java

Form

Representation

Attribution — Origin / Source

Collected by Matthias Hauswirth — Own practice

Conceptual Advantage

Makes explicit that arrays are heap objects where elements are accessed by index, and where the length is stored in a special read-only field. Particularly useful when explaining arrays of arrays.

Mapping

PLNM
... (all aspects of 1 - Objects, 2 - Stack Frames, 3 - References)
array red rounded rectangle on the heap, labeled with array type
array element a field in the array, with index, type, and value
array length a field in the array, with name 'length' and type int, and a value corresponding to the number of elements

Draws Attention To

Arrays.

Use When

Introducing the concept of array, and arrays of arrays.

Cost

Only a minor addition to the cost of the prior variant of this notional machine.

Details

We use this notional machine in instruction as well as assessments (e.g., clicker questions, mastery checks, exams). For instruction, the instructor draws the representation. For assessment, the student draws the representation.

The name of this notional machine is inspired by two of the three regions that make up the memory of the runtime environment of imperative programming languages:

The third area, “globals”, could be added to the diagram, but it is less important when using an object-oriented style where static members are less prevalent.

While we primarily use this notional machine for Java, it could easily be adapted for other imperative object-oriented programming languages, such as C# or Python.

Notation

Objects, Stack Frames, and References

Arrays

Origins of Notation

The notation for this notional machine is based on how the BlueJ IDE represents objects, with a few differences:

Stack & Heap Diagrams in Informa Clicker

The Informa Clicker tool, a software-based classroom response system, provides a question type with an integrated editor for stack and heap diagrams.

Check this research paper

Matthias Hauswirth and Andrea Adamoli
Solve & Evaluate with Informa: A Java-based Classroom Response System for Teaching Java
PPPJ '09

Constructor Calls

Local Variables

Method Calls

Web-Based Stack & Heap Diagram Editor

Davide Ciulla developed a prototype of a web based stack and heap diagram editor:

Comments or Feedback?

Do you have feedback on this notional machine? Did you find a mistake, or do you have a request for improvement? You can create an Issue on GitHub, where the description is hosted. This way we can see your feedback and address it.

For this, you need a GitHub account. Then follow this link to see the source file of this page. In there, click the ... left of the highlighted line, then pick "Reference in a new issue".

Create an Issue on GitHub