Object Reference and Arrays

The standard eight primitive variable types are:

boolean char byte short int long float double

a mnemonic to remember this is:

Be careful!

Bears

Shouldn’t

Ingest

Large

Furry

Dogs

I think I’m beginning to understand object references in Java as well as arrays. Object references use a dot where you use the thing before the dot to get to the thing after the dot. For example:

myCat.meow();

means use (the object referenced by) “myCat” to invoke the “meow()” method.

In order to 1. declare an object, 2. create an object,  and 3. assign the object there are 3 steps:

1st, declare a reference variable:

Cat myCat   — the reference variable is named myCat, and the variable is forever of the type Cat.

2nd, create an object

new Cat();   — tells the java virtual machine to allocate space for a new Cat object on the heap

3rd, link the object and the reference

Cat myCat = new Cat();   — Assigns the new Cat object to the reference variable myCat

As references are modified throughout an application, it may turn out that an object that used to refer to one object no longer refers to that object and now refers to another object. But that does not mean that the object reference has disappeared, nor the object that it originally referred to. However, abandoned objects that are left on the heap are fodder for the garbage collector. Also references can point to Null which means for example, the reference myCat could point to Null, and it just sits there. References aren’t collected as garbage because later a different object could still be assigned to it.

Arrays themselves are always objects, and can be declared just like object references to hold primitives or other object references.

Here’s an example of how to create an array of cats:

1. Declare a Cat array variable (the brackets are what defines Cat as an array)

Cat[] pets;

Cat[] is the type of the array, and pets is the variable. ( this is where I get confused)…

2. Create a new Cat array with a length of 6, and assign it to the previously-declared Cat[] variable pets

pets = new Cat[6];

3. The objects are still missing so let’s create new Cat objects and assign them to the array elements.

pets[0] = new Cat();

pets[1] = new Cat();

pets[2] = new Cat();

So now pets[0, 1, 2] refer to a Cat() object, however, the other 3 pets do not refer to anything until we declare that, in fact, pets[3, 4, 5] = either a new cat object, or a different existing object, or null (a reference variable that is not referencing any object has a value of null).

Take for example two different ways of creating 10 contact objects:

Contact[] ca = new Contact[10];

while (x < 10) {

ca[x] = new Contact();

x = x + 1;

}

_________________________________

Contact refc;

while ( x < 10 ) {

refc = new Contact();

x = x + 1;

}

In this second method of creating 10 contact objects, only the last of the Contact objects that was created will be accessible. With each trip through the loop, an assignment of a new object to the one reference variable was made. All the other previously referenced objects were abandoned on the heap – unreachable. The second method of creating the array of 10 contact objects is pretty useless.