CMUQ 15-121 Arrays of Objects
1. Introduction
In these notes we’ll discuss arrays of objects and how they are stored.
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
object 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 object. It has a name
, age
, and basic constructor.
Now, let’s look at some sample code that makes use of our Person
class. Note that all we do is 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 a like a small pointer to the actual object. Since this code doesn’t actually 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:
That’s not very interesting, but it does at least show us that creating a variable of an object type doesn’t actually 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 actually allocates a Person
object using the new
keyword. Going back to our diagram, now will look like this:
Now, our myPerson
variables points to an allocated Person
object, which also includes an allocated String
.
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. Actually, 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 actually 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:
So, now one 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: