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.

A Software Engineer Learns Java and Object Orientated Programming
titlepage.xhtml
part0000_split_000.html
part0000_split_001.html
part0000_split_002.html
part0000_split_003.html
part0000_split_004.html
part0000_split_005.html
part0000_split_006.html
part0000_split_007.html
part0000_split_008.html
part0000_split_009.html
part0000_split_010.html
part0000_split_011.html
part0000_split_012.html
part0000_split_013.html
part0000_split_014.html
part0000_split_015.html
part0000_split_016.html
part0000_split_017.html
part0000_split_018.html
part0000_split_019.html
part0000_split_020.html
part0000_split_021.html
part0000_split_022.html
part0000_split_023.html
part0000_split_024.html
part0000_split_025.html
part0000_split_026.html
part0000_split_027.html
part0000_split_028.html
part0000_split_029.html
part0000_split_030.html
part0000_split_031.html
part0000_split_032.html
part0000_split_033.html
part0000_split_034.html
part0000_split_035.html
part0000_split_036.html
part0000_split_037.html
part0000_split_038.html
part0000_split_039.html
part0000_split_040.html
part0000_split_041.html
part0000_split_042.html
part0000_split_043.html
part0000_split_044.html
part0000_split_045.html
part0000_split_046.html
part0000_split_047.html
part0000_split_048.html
part0000_split_049.html
part0000_split_050.html
part0000_split_051.html
part0000_split_052.html
part0000_split_053.html
part0000_split_054.html
part0000_split_055.html
part0000_split_056.html
part0000_split_057.html
part0000_split_058.html
part0000_split_059.html
part0000_split_060.html
part0000_split_061.html
part0000_split_062.html
part0000_split_063.html
part0000_split_064.html
part0000_split_065.html
part0000_split_066.html
part0000_split_067.html
part0000_split_068.html
part0000_split_069.html
part0000_split_070.html
part0000_split_071.html
part0000_split_072.html
part0000_split_073.html
part0000_split_074.html
part0000_split_075.html
part0000_split_076.html
part0000_split_077.html
part0000_split_078.html
part0000_split_079.html
part0000_split_080.html
part0000_split_081.html
part0000_split_082.html
part0000_split_083.html
part0000_split_084.html
part0000_split_085.html
part0000_split_086.html
part0000_split_087.html
part0000_split_088.html
part0000_split_089.html
part0000_split_090.html
part0000_split_091.html
part0000_split_092.html
part0000_split_093.html
part0000_split_094.html
part0000_split_095.html
part0000_split_096.html
part0000_split_097.html
part0000_split_098.html
part0000_split_099.html
part0000_split_100.html
part0000_split_101.html
part0000_split_102.html
part0000_split_103.html
part0000_split_104.html
part0000_split_105.html
part0000_split_106.html
part0000_split_107.html
part0000_split_108.html
part0000_split_109.html
part0000_split_110.html
part0000_split_111.html
part0000_split_112.html
part0000_split_113.html
part0000_split_114.html
part0000_split_115.html
part0000_split_116.html
part0000_split_117.html
part0000_split_118.html
part0000_split_119.html
part0000_split_120.html
part0000_split_121.html
part0000_split_122.html
part0000_split_123.html
part0000_split_124.html
part0000_split_125.html
part0000_split_126.html
part0000_split_127.html
part0000_split_128.html
part0000_split_129.html
part0000_split_130.html
part0000_split_131.html
part0000_split_132.html
part0000_split_133.html
part0000_split_134.html
part0000_split_135.html
part0000_split_136.html
part0000_split_137.html
part0000_split_138.html
part0000_split_139.html