Skip to content

Memory Diagrams

Drawing pictures is helpful to understand how a program runs. A memory diagram is a picture that shows the contents of memory at a certain point in time. Specifically, a memory diagram shows variables (on the left) and objects (on the right).

Variables

A variable is drawn as a small box. The variable's name is on the left, and the variable's value is inside the box. The variable's type (optional) is above the box. For example, int count = 159; would be drawn as follows:

Java has eight primitive data types: byte, short, int, long, float, double, boolean, char. All other data types are defined by classes that represent objects. A reference is an integer that "points to" an object in memory. For example, a String variable refers to a String object:

As shown above, a reference is drawn as an arrow. The value of the mood variable is the memory location of the "happy" object. In contrast, the value of the count variable is the integer 159.

Note: null is not an object. The value null indicates a variable does not refer to an object. For example, Scanner in = null; would be drawn as follows:

Methods

A method is drawn as a large box. The method's name is on the left, and the method's variables are inside the box. For example, the diagram below shows the state of the program just before calling System.out.println():

public static void main(String[] args) {
    int hour = 11;
    int minute = 59;
    printTime(12, 15);
}

public static void print(int hour, int minute) {
    System.out.println(hour + ":" + minute);
}

Technically, main() also has a variable named args. But since command-line arguments are rarely used, the args variable is usually omitted from memory diagrams.

Arrays

An array variable refers to an array object. An array object is drawn as contiguous boxes. Indexes (optional) can be shown below the boxes. For example, double[] data = {1.2, 3.4, 5.6} is drawn as follows:

Each array element (such as data[0]) acts like a variable. If the element type is primitive, then the value is in the box. If the element type is a reference, then the value is an arrow. For example, String[] words = {"hello", "world"} looks like this:

Objects

An object is drawn as a large box. The object's attributes (variables) are inside the box. The object's type (optional) is above the box. For example:

public static void main(String[] args) {
    Point p1 = new Point(11, 12);
    Point p2 = new Point(13, 14);
}

Internally, a String object is an array of bytes. So the details of String pet = "dog"; could be drawn as follows:

For simplicity, String objects are usually drawn as a smaller box with a string in quotes. In fact, any object can be drawn as a String based on the object's toString() format:

public static void main(String[] args) {
    String pet = "dog";
    Point p1 = new Point(11, 12);
}

Static

static variables are initialized when a class is used for the first time, often before the main() method is called. Each class containing static variables is drawn on the left of the diagram. The following example shows two objects of the same class:

public class Person {

    private static int count = 0;

    private String name;
    private int pin;

    public Person(String name, int pin) {
        count++;
        this.name = name;
        this.pin = pin;
    }

    public static void main(String[] args) {
        Person p1 = new Person("Taylor", 1989);
        Person p2 = new Person("Travis", 8713);
    }

}

Note

The memory diagrams on this page are similar to, but not identical to, the diagrams drawn by Java Visualizer. Compare, for example, the Person diagram above with this visualization.