Primitive types and object types

By now, we are used to distinguishing between primitive values and objects. We know, for instance, that subtyping is provided for object types but not primitive types. In this chapter, we will focus a bit more in-depth on the differences between these two kinds of values. We will look at special types, called wrapper classes, that provide bridges between primitive values and objects. We will also study the difference between the way these two kinds of values are stored in memory, and the implication this difference has for the assignment statement. This will lead us to the concept of garbage collection, an important feature of Java.
Storing Object Values and Variables:
Object values and variables, however, are stored differently. Consider:
Integer I = new Integer (5);
Double D = new Double(5.5);
As before, both values and the variables are allocated memory. However, each assignment copies into the variable’s block, not the contents of the value block, but instead its address. All Java addresses are 1 word long, so all variables are allocated a 1-word block, regardless of their types. Thus, both the Double variable, D, and the Integer variable, I, are the same size, which was not the case with the double variable, d, and integer variable, i, we saw above.

All objects, however, are not of the same size. When a new object is created, a composite memory block consisting of a series of consecutive blocks, one for each instance variable of the object, is created. Thus, assuming an Integer has a single int instance variable, a block consisting of a single integer variable is created. Similarly, for a Double instance, a block consisting of a single double instance variable is created. The sizes of the two objects are different because of the difference in the sizes of their instance variable. However, in both cases, the object block consists of a single variable.

Now consider the following class:
public class APoint implements Point {
int x,y;
public APoint (int initX, int initY) {
x = initX; y = initY;
}
public void setX(int newVal) {
x = newVal;
}

}
The figure below shows how the following assignment of an instance of this class is processed:
Point P = new APoint( 50, 100) ;
An instance of this class has a memory block consisting of two consecutive int blocks, as shown in the figure.

 

Object Values and Variables:
Now consider the following subclass of APoint, called ABoundedPoint, that declares two APoint instance variables defining a rectangular area defining user-specified bounds of the point:
public class ABoundedPoint extends APoint {
APoint upperLeftCorner, lowerRightCorner;
public ABoundedPoint (int initX, int initY, Point initUpperLeftCorner, Point initLowerRightCorner) {
super(initX, initY);
upperLeftCorner = initUpperLeftCorner;
lowerRightCorner = initLowerRightCorner;
}

}
Recall that an instance has not only the instance variables defined in its class but also those defined the superclasses of its class. Therefore, an instance of ABoundedPoint, has a memory block consisting of memory blocks of four variables, two int variables, each of size 1 word, inherited from APoint, and two object variables, each also of size 1 word, defined in ABoundedPoint.

 

 

Comments are closed.