CPSC 124, Spring 2013
Sample Answers to Quiz #6

Question 1. Explain the meaning of "final" in the following declaration, and give at least one good reason why doing something like this would be desirable.

      public final static int SIZE = 40;

Answer. When applied to the declaration of a variable, the modifier final means that the value stored in that variable can not be changed after the variable has been initialized (for as long as the program is running). This makes the variable SIZE into a "named constant." It can be a good idea to use a named constant because it allows you to use a meaningful name (SIZE in this example) in the program instead of an arbitrary-looking literal (40 in this example). Another reason is that it will be easy to change the value of the constant (between program runs!): You just have to modify the value in the one line of the program where the constant is defined, instead of tracking down all the places in the program that use the value and modifying each occurrence.

Question 2. Consider the class shown below. Write a Java code segment that will do the following: Declare a variable of type Point3D; create an object of type Point3D and assign it to the variable; and set the values of the three instance variables in the object to be 3.14, 2.72, and 0.68.

public class Point3D {
    double x;
    double y;
    double z;
}

Answer.

Point3D  point;  // (Any name would be OK.)
point = new Point3D();
point.x = 3.14;
point.y = 2.72;
point.z = 0.68;

Note that the first two lines could be combined to:

Point3D point = new Point3D();

Question 3. "A variable in Java can never hold an object, only a pointer to an object." What does this mean? Draw a picture to illustrate your answer. (Your answer should include an explanation of the term pointer. For the picture, you might use the example from the previous problem. And you might mention the heap.)

Answer. When an object is created (with the new operator), memory to hold the object's data is allocated in a part of memory called the heap. The object is not stored in a variable. Instead, a variable can hold the address of the location in memory where the object is stored. The address is called a pointer or reference to the object. For example, the code segment in the answer to the previous problem creates a situation that looks something like this:

                               /--------------------\
  point: o-------------------> | [ class: Point3D ] |
                               |                    |
                               | x:  3.14           |
                               | y:  2.72           |
                               | z:  0.68           |
                               |                    |
                               \--------------------/

The object, created by "new Point3D()" is the box on the right, representing a block of memory in the heap. The variable, point, on the left, somewhere else in memory. The value shown as an arrow is the pointer, which is really just an address -- a number -- in the variable that gives the location of the object.