Previous Table of Contents Next


14.2.8.2. Accessing Objects

As you’ve probably noticed in various example code fragments by now, the way you access the fields of an object is with a dot:

   ComplexNumber c = new ComplexNumber();
   c.x = 1.0;
   c.y = -1.414;

This syntax is reminiscent of accessing the fields of a struct in C. Recall, though, that Java objects are always accessed by reference, and that Java performs any necessary dereferencing for you. Thus, the dot in Java is more like -> in C. Java hides the fact that there is a reference here in an attempt to make your programming easier. The other difference between C and Java when accessing objects is that in Java you refer to an object’s methods with the same syntax used for fields:

   ComplexNumber c = new ComplexNumber(1.0, -1.414);
   double magnitude = c.magnitude();

14.2.8.3. Garbage Collection

Objects in Java are created with the new keyword, but there is no corresponding old or delete keyword or free() method to get rid of them when they are no longer needed. If creating an object with new is like calling malloc() in C or using new in C++, then it would seem that Java is full of memory leaks, because we never call free() or use the delete operator.

In fact, this isn’t the case. Java uses a technique called garbage collection to automatically detect objects that are no longer being used (an object is no longer in use when there are no more references to it) and to free them. This means that in our programs, we spend less time worrying about freeing memory and destroying objects—the garbage collector takes care of that.

If you are a C or C++ programmer, it may take some getting used to to just let allocated objects go without worrying about reclaiming their memory. Once you get used to it, however, you’ll begin to appreciate what a nice feature this is. We’ll discuss garbage collection in more detail later.

14.2.9. Arrays

Most of what we learned in the previous sections about reference types and objects applies equally well to arrays in Java:

  Arrays are manipulated by reference.
  They are dynamically created with new.
  They are automatically garbage collected when no longer referred to.

The following subsections explain these and other details.

14.2.9.1. Creating and Destroying Arrays

There are two ways to create arrays in Java. The first uses new, and specifies how large the array should be:

   byte octet_buffer[] = new byte[1024];
   Button buttons[] = new Button[10];

Since creating an array does not create the objects that are stored in the array, there is no constructor to call, and the argument list is omitted with this form of the new keyword. The elements of an array created in this way are initialized to the default value for their type. The elements of an array of int are initialized to 0, for example, and the elements of an array of objects are initialized to null. This last point is important to remember: Creating an array of objects only allocates storage for object references, not objects themselves. The objects that will be referred to by the elements of the array must be created separately.

The second way to create an array is with an initializer, which looks just like it does in C:

   int lookup_table[] = {1, 2, 4, 8, 16, 32, 64, 128};

This syntax dynamically creates an array and initializes its elements to the specified values. The elements specified in an array initializer may be arbitrary expressions. This is different than in C, where they must be constant expressions.

In Java 1.1, arrays may also be created and initialized “anonymously” by combining the new syntax with the initializer syntax. It looks like this:

   Menu m = createMenu(“File”, new String[] { “Open...”, “Save”,
   “Quit” });

Arrays are automatically garbage collected, just like objects are.


Previous Table of Contents Next