Java

TOPIC 46 – OBJECT ARRAYS

 

 

LESSON NOTE

 

 

INTRO

 

An object array is simply an array that contains objects.  They mostly work in the same way as regular arrays.  However, there are a few things to learn or to remember from the OOP unit.

 

INITIALIZING OBJECTS

 

If we type in the following code, we get an error:

 

         Rectangle r;

         System.out.println(r);  //error

 

The reason we get an error is that object r has not been initialized which really means that we haven’t created an object for r to refer to.

 

The solution is often to simply create the object like in the code below:

 

         Rectangle r = new Rectangle(5, 8);

         System.out.println(r);

 

NULL

 

However, sometimes we do not want to create an object.  Sometimes, we don’t know what information to put in an object yet. 

 

In such a situation, we can simply make the object be equal to null.  Null means that the object doesn’t exist in memory.  However, it is considered initialized and can be outputted to screen.

 

         Rectangle r = null;

         System.out.println(r);  //outputs null

 

Note that an object that is null can also be changed afterwards.

Let’s consider the following code and analyze what happens:

 

Point p = null;

p = new Point(2, 8);

 

After the first statement, we have the following in memory:

 

 

And after the second statement, we then have:

 

 

NULL = DEFAULT OBJECT VALUES IN ARRAYS

 

We can create an array of any type of object.  By default, each element in the array is set to null.

For example, consider the following code where we create an array of Rectangle objects. 

         Rectangle[] r = new Rectangle[5];

         System.out.println(Arrays.toString(r));

 

When we output that array to screen, it outputs a null for each element:

 

         [null, null, null, null, null]

 

So, when we want to create object arrays, there are two steps to follow.

 

CREATING OBJECT ARRAYS

 

Here are the two steps to follow to fully create and initialize object arrays:

 

          Step #1 – Declare and create array.

 

          Step #2 – Create each object. 

 

EXAMPLE – CREATING A POINT ARRAY

Here are the statements needed to create a Point array of size 3.

 

Point[] pArray = new Point[3];  //Step #1

 

The above statement will create an array of references.  However, since each element (object) is not created, the array references are null.  Here is what this looks like in memory.

 

 

After creating the array, we initialize/create the object for each element by using:

 

pArray[0] = new Point(4, 7);

pArray[1] = new Point(2, 8);

pArray[2] = new Point(-3, 4);

 

After the above statements, the structure would look like this:

 

 

We can now access any Point object by using the array name and the index.  For example, to get the x value of the Point at index 0, we use:

 

int xValue = pArray[0].getX();

 

The following code would output the x and y coordinate of each Point element in the array.

 

for (int i = 0; i < pArray.length; i++)

{

    System.out.println("(" + pArray[i].getX() + ", " + pArray[i].getY() + ")");

}

 

NULL POINTER EXCEPTIONS (ERRORS)

 

If you forget to create the objects (a step that many students overlook at first), you will get a null pointer exception.  Note that pointer is simply another word for reference.

 

For example, the following statements lead to a null pointer exception:

 

     Point[] pts = new Point[5];

     System.out.println(pts[0].getX());

 

The first line creates an array of Point object references that are all null.  When you try to call a method of a null object, you get a null point exception.