Arrays of Objects



1. Introduction

In these notes, we’ll discuss arrays of objects and how they are actually stored in memory.

2. How Objects Are Stored

Before we can look at how an array of objects are stored, let’s first discuss how objects, in general, are stored. For the rest of the examples on this page, consider the Person class given here:

public class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

A person is a fairly straightforward class. It has a name, age, and basic constructor.

Now, let’s look at some sample code that makes use of our Person class. The only thing we are going to do here is to define a variable of type Person.

public class ObjectStorageDemo {

    public static void main(String[] args) {
        Person myPerson;
    }

}

When you do this, Java allocates a variable that references a Person. A reference is like a small pointer to the actual object. Since this code doesn’t create a Person object (it only creates a reference to one) then Java sets that reference to null. Setting it to null means that the variable doesn’t currently reference an actual object, but instead points to nothing. This could be illustrated as:

Computers can’t store “nothing” in memory. Everything is a number. In this case, null is just another way of saying 0.

This illustrates that creating a variable of an object type doesn’t allocate an object. Instead, it only allocates a reference. Now, let’s make our code just a bit more complicated:

public class ObjectStorageDemo {

    public static void main(String[] args) {
        Person myPerson;
        myPerson = new Person("John", 36);
    }

}

Notice that this code allocates a Person object using the new keyword. Going back to our diagram, now it will look like this:

Our myPerson variable points to an allocated Person object, which also includes an allocated String. (And since String is also an object, it is also a reference.)

3. How Arrays of Objects are Stored

Now that we’ve covered the idea of references to individual objects, let’s talk about arrays of objects. Once again, here is some sample code:

public class ObjectStorageDemo {

    public static void main(String[] args) {
        Person[] myPeople;
    }

}

Here you can see that we create an array of Person objects. We haven’t created an array yet. We’ve created a reference to an array. Our diagram looks like this:

Now, let’s actually allocate the array itself:

public class ObjectStorageDemo {

    public static void main(String[] args) {
        Person[] myPeople;
        myPeople = new Person[3];
    }

}

By calling new and allocating an array, we get this:

Notice that we now have an array, but it isn’t of objects: It is an array of references to objects. And each one is null because we haven’t allocated the actual Person objects yet.

So, let’s go ahead and allocate one of the Person objects:

public class ObjectStorageDemo {

    public static void main(String[] args) {
        Person[] myPeople;
        myPeople = new Person[3];
        
        myPeople[0] = new Person("John", 36);
    }

}

This gives us:

The first of the three Person references in the array points to something, but the other two still store null.

Let’s finish filling up the array:

public class ObjectStorageDemo {

    public static void main(String[] args) {
        Person[] myPeople;
        myPeople = new Person[3];
        
        myPeople[0] = new Person("John", 36);
        myPeople[1] = new Person("Ahmed", 23);
        myPeople[2] = new Person("Zhang", 64);
    }

}

Now, with all entries filled in, we have:

The take-away from all of this is that it is important to understand when references are created as opposed to when objects themselves are created.