11
Object References
The previous few chapters have introduced the concepts of objects and classes. This chapter will look at objects in more detail, specifically; you will look at the difference between objects, and the variables that hold references to these objects. Up until now I have not necessarily distinguished these two concepts, but learning to distinguish them is an essential aspect of learning Java.
Consider the following line of code:
StringBuffer sb1 = new StringBuffer("Hello World");
This line looks simple enough: it has instantiated a StringBuffer object, and assigned it the text “Hello World”. Additionally, after this line of code executes, a variable called sb1 can be used for accessing that object.
It may be tempting to assume that sb1 and the object created are one and the same thing: for instance, that sb1 is simply the name assigned to the newly created object. This is not, however, an accurate interpretation of this line of code: sb1 is a reference to an object.
In order to make the distinction clear, consider the impact of adding the following 2 lines immediately after the line above:
StringBuffer sb2 = sb1;
sb2.append("!!!");
sb1 and sb2 are now both referring to the same object. Only a single object has been created (an object can only be created via the new keyword), but both these variables are referring (or pointing) to this object.
For this reason, when I append “!!!” to the StringBuffer referenced by sb2, the extra text will be seen when I print out the value of sb1 or sb2:
System.out.println(sb1);
System.out.println(sb2);
Both of these lines will print the same value because they are both printing the value of the same object.
It is possible to use the == operator to determine if two references refer to the same object, for instance, the following expression will return true:
sb1 == sb2;
The best way to visualize the previous example is via the drawing in figure 11-1:
Figure 11-1
With this understood it is now possible to explain the default behavior of the equals method defined on the Object class; this method returns true if two references are compared that refer to the same underlying object:
sb1.equals(sb2);
The default equals method, therefore, is the same as using ==. In later chapters you will look at how you can provide your own implementation of this method.
In some programming languages the distinction between objects and the references to them is made explicit, and it is possible to access either the reference or the underlying object. In Java this is not possible you can only access objects via their references.
Confusingly primitives do not act in this manner. Consider the lines below:
int num1 = 2;
int num2 = num1;
num2 = 5;
System.out.println(num1);
System.out.println(num2);
If primitives behaved like objects you would expect that this example would print out the number 5 twice. In fact it prints out the following:
2
5
When you declare a variable as a primitive type, the variable (e.g. num1) is not a reference to the primitive; the variable and the value are one and the same thing. It is not therefore possible for two variables to refer to the same primitive value (although, obviously, they can have the same value).
Another way to think about the distinction between objects and references is that both the object and the reference are occupying independent areas of computer memory. The reference stores the memory address of the referenced object in its memory: therefore if two references hold the same memory address, they are referring to the same object.
In the case of a primitive, the only memory that is allocated is the memory required by the primitive type (for instance, 64 bits for a long), and any time the primitive variable is accessed, this memory is accessed directly.