Tino APCS

Object References

Recall from Lesson A2 that an object is constructed as an instance of a particular class. Objects are referenced using an identifier called a reference variable. Unlike primitives, which directly store their data in the associated variable, object references store only a reference to where the data can be found in memory. Now that we are working with a simple class, String, it is a good time to discuss object references.

Whenever the new operator is used, a new object is created. Each time an object is created, it is stored in some address in memory and that address can be saved in a reference variable to be accessed later. The reference is used to find the object, much like a street address is used to find a location where someone lives and stores all their stuff. When creating an object reference in Java, the variable stores the memory location where the object can be found rather than store the contents of the object directly.

It is possible to store a new object reference in a variable. For example:

String str;

str = new String("first string");
System.out.println(str);

str = new String("second string");
System.out.println(str);


Run Output:

first string
second string


In the example above, the variable str is used to store a reference to a String object that stores the text, “first string”. In the second part of the example a reference to a String that stores the text, “second string” is stored in the variable str. If another reference is saved in the variable, it replaces the previous reference because each reference variable can only point to one object at any given time (see diagram below).

If a reference to an object is no longer being used then there is no way to find it, and it becomes "garbage." The word "garbage" is the correct term from computer science to use for objects that have no references. This is a common situation when new objects are created and old ones become unneeded during the execution of a program. While a program is running, a part of the Java system called the "garbage collector" reclaims each lost object (the "garbage") so that the memory is available again. In the above example, the String object “first string” becomes garbage.

Multiple objects of the same class can be created by using the new keyword.

String strA; // a reference variable
String strB; // another reference variable

// create the first object and save its reference in a reference variable
strA = new String("first string");

// print data referenced by the first object.
System.out.println(strA);

// create the second object and save its reference in a reference variable
strB = new String("second string");

// print data referenced by the second object.
System.out.println(strB);

// print data referenced by the first object.
System.out.println(strA);


Run Output:

first string
second string
first string


This program has two reference variables, strA and strB. It creates two objects and places a reference to each object in one of the variables. Since each object has its own reference variable, no reference is lost, and no object becomes garbage (until the program has finished running).

Different reference variables that refer to the same object are called aliases. In effect, there are two names for the same object. For example:

String strA; // declare a reference variable
String strB; // declare another reference variable

// Create the only object and save its
// reference in strA
strA = new String("only one string");
System.out.println(strA);

strB = strA; // copy the reference to strB.
System.out.println(strB);


Run Output:

only one string
only one string


When this program runs, only one object is created by the new keyword. (Technically, a String literal is also created but no reference to it is saved - see below for more about this.) Information about how to find the object (its memory address) is stored in strA. The assignment operator in the statement

strB = strA; // copy the reference to strB


copies the address stored in strA to strB. It does not make a copy of the actual object. It is like writing the address to your house on two different pieces of paper - doing so does not create two houses.

String Pool and Counting String Objects

A String literal is any hardcoded string such as "I love Java." Even when reference variables are used, the portion in quotations is a String literal. For example,

String str = new String("Hello");


passes the String literal "Hello" into a String constructor.

In order to save resources, Java keeps track of String literals in a String Pool. The String Pool is an area of memory used to store exactly one copy of each String literal. In this way, Java eliminates storing duplicates of the same String values. Consider this example:

String strA = "only one string";
String strB = "only one string";
System.out.println(strA == strB); // Prints true


When Java encounters "only one string" the first time, it adds "only one string" to the String Pool and the reference variable 'strA' points to it. When Java sees the second occurrence of "only one string" it looks in the String Pool table and sees that there is already an instance of "only one string" so another copy is not created. Instead, the reference variable 'strB' points to the existing "only one string" in the String Pool.

How many objects are created in the previous example? Only one object is created because both 'strA' and 'strB' refer to the same copy of "only one string" stored in the String Pool.

However, Java always creates a new object when a constructor is used. Consider this example:

String one = "Hello";
String two = new String("Hello");
System.out.println(one == two); // Prints false


In this case, two objects are created: One object for the two String literals ("Hello") and a second object to store the object 'two.'

Behind the scenes, Strings are stored as immutable (unchangeable) character arrays. In the case of String 'two' the character array used will point back to the original "Hello" but the overall object in which this happens is a separate address from the String literal.

Here is another example:

String str = new String("HI");
System.out.println(str == "HI"); // prints false (str is not the same object as "HI")
System.out.println(str.equals("HI")); // prints true (str stores the same characters in the same order as "HI")
// Two objects are created, but only one object is accessible


The literal "HI" creates a String object that says "HI" and stores it in the string pool. The new keyword also creates a String object and stores a reference to it in the variable str. The String str references the same character array as the literal "HI", so it contains the same characters in the same order, but str does NOT reference the same object as the String literal "HI". Thus, two objects are created, but only one is accessible with a reference variable (the object str is pointing to).

Want to know more? Search for "Java String Pool" online or see the section on String Literals in the Java Language Specification.

Dark Mode

Outline