Reference Data Types

Reference data types refer to the value but do not contain the value themselves. Reference data types are the cornerstone of object oriented programming. They are the secret sauce behind Java’s popularity in the late 90s. A reference data type is inherently different from primitive data types because MOST (and not all) reference types are created by user to solve a real world problem. A reference data type is a convenient and cheap way to pass large chunks of data to and fro.

What are Reference Types?

A prime example of reference data type would be any user generated class. When you create a class such as the one below, Java stores the details of the class in the method area.

123456789101112131415public class Politician {   private BankAccount[] storageArea;  … // more such fields   // assume Take is another refernce type  // possibly the currency of Utopia  public void robPeople(Take peoplesMoney) { }   // publicly rob and return void   public void ripoff(Take peoplesMoney) { }   // publicly ripoff and return void  …. }

When you create an instance of the class such as in the code below, you are creating a variable of reference data type.

123Politician primeMinister = new Politician(); // primeMinister is a reference

The key distinction is the fact is demonstrated in the code below

1234567891011Politician partyFunctionary = new Politician();System.out.println(partyFunctionary == primeMinister);// above returns false  partyFunctionary = primeMinister;// now the partyFunctionary and all his bank accounts// pass into oblivion// the party functionary takes over as prime ministerSystem,out.println(partyFunctionary == primeMinister);// now it's true

What is a Java Reference?

Java objects are allocated on heap. As discussed in JVM Internals 101, heap is a shared memory. All Java threads can access an object on the heap. This access happens through a handle. This handle is the reference. It’s almost equivalent to pointers in C and references in C++.

The name of the variable, such as primeMinister in this example, acts as the references. It is a way of reaching the Politician object lying on heap. Therefore, Java references are a handle to dynamically allocated objects.

What Does Java Reference Contain?

A Java reference contains two key bits of information –

  1. Address of the object on the heap
  2. Address in the method area of class information of the type the reference is pointing to.

Copying Objects and References

Shallow Copy

In the code above, when we said partyFunctionary = primeMinister, we did not make a copy of the primeMinister object. We made a copy of reference of the primeMinister object. Once the operation was over, both references started pointing to the same object. Changes made to the object through one of the references is visibile through the other. So if you manipulate primeMinister’s bankAccount it is same as changing the bankAccount through partyFunctionary.

This kind of assignment where only a reference is copied but the underlying object is not copied is called shallow copy. Note that the original partyFunctionary (the one created through new operator, before reference assignment), is lost for ever. Since there is no handle (reference) left to that object, it is elgible for garbage collection.

Deep Copy

Sometimes, you may be required to create a copy of the entire underlying object. This is called deep copying an object. Since objects encapsulate the state, deep copy essentially means capturing the state of an object or taking its snapshot. A deep copy involves copying the value of all instance variables. If the instance variables themselves are of reference type, a true deep copy involves deep copying them as well.

As you can see, this can quickly become a very costly operation. You may find that you are duplicating large parts of your memory. Therefore, think twice before starting out with deep copy.

Since deep copy is a rare operation, Java does not provide any library method of doing it. DO NOT use or override clone() method. It is considered bad form. Instead, use copy constructors. Another option, if you are willing to make your class Serializable, is to serialize an object and store it else where while deserializing.

Leave a comment

Your email address will not be published. Required fields are marked *