JMU
Objects and References
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review of Data Types in Java
Type Handling in General
A Special Reference
The . Operator
Some Java Internals

An Example of a Primitive Type
(A Fragment of main())

javaexamples/basics/MemoryAllocationExample1.java (Fragment: 0)
        // Add the name d to the name table, allocate eight bytes of
        // memory (since doubles use eight bytes), and associate the
        // allocated memory with that name.
        //
        double   d;
        
        
        // Put the value 5.0 in the eight bytes of memory
        // that are associated with the name d.
        //
        d = 5.0;
        
        
Some Java Internals (cont.)

An Example of a Reference Type
(A Fragment of main())

javaexamples/basics/MemoryAllocationExample1.java (Fragment: 2)
        // Add the name c to the name table, allocate four bytes of
        // memory (since addresses use four bytes), and associate the
        // allocated memory with that name.
        //
        Color    c;
        
        
        // Allocate 12 bytes of memory to hold the three int
        // attributes in the Color object and put the int values in
        // that memory.  Then, put the address in the four bytes
        // associated with the variable named c.
        //
        c = new Color( 69,  0, 132);
        
        
Memory Allocation
A Conceptual Model of Memory
The new Operator and Memory
Visualization of Memory
images/JavaMemoryModel.png

Note About the Heap, Literals space, and Class space: We will sometimes worry about the size of entities and work directly with the bytes and will sometimes work with larger entities and not worry about the bytes they occupy.

An Example of Static Memory Allocation

In this example (from a fragment of main()), the same memory is used for the variables during each iteration.

javaexamples/basics/MemoryAllocationExample2.java (Fragment: 0)
       int   i, j, n;
       
       n = 3;
       
       j = 1;
       for (i=0; i < n; i++)
       {
          j = j + i + 1;
       }
        
An Example of Static Memory Allocation (cont.)
images/JavaMemoryModel_StackExample.png
An Example of Static and Dynamic Memory Allocation

In this example (from a fragment of main()), memory is allocated for a Color object each iteration. (Reminder: A Color object has three int attributes, named red, green, and blue.)

javaexamples/basics/MemoryAllocationExample4.java (Fragment: 0)
        int     i, n;                // Compile-Time
        Color   c;                   // Compile-Time


        n = 3;

        for (i=0; i < n; i++)
        {
            c = new Color(i, i, i);    // Run-Time
        }
        
An Example of Static and Dynamic Memory Allocation (cont.)
images/JavaMemoryModel_HeapExample.png

Note: In this example we are not worrying about the size of the three int attributes in the Color objects.

Assignment
Assignment to final Variables
Reference Variables Declared to be final

Consider the following class:

javaexamples/basics/Pair.java (Fragment: 0)
public class Pair
{
    public int  a, b;
}
        

And the following fragment (in main()):

       final Pair      p;
       

       p   = new Pair();
       p.a = 0;
       p.b = 100;
       
       // This line will compile
       p.a = 50;
       

       // This line will not compile
       p = new Pair();
    
Aliases

The following example (from a fragment of main()) uses the assignment operator both during the instantiation of an object and to create an alias.

javaexamples/basics/AkaExample1.java (Fragment: 0)
        Color    c, d;


        // The left-hand-side is a reference variable so
        // the right-hand-side must be (and is) a
        // memory address
        c = new Color(102, 102, 0);
        
        
        // The memory address on the right-hand-side is
        // assigned to the reference variable on the
        // left-hand-side
        //
        d = c;
        
Aliases (cont.)
javaexamples/basics/Pair.java (Fragment: 0)
public class Pair
{
    public int  a, b;
}
        

In the following example (from a fragment of main()), p and q refer to the same object so changing the attributes of one changes the attributes of "the other".

javaexamples/basics/AkaExample3.java (Fragment: 0)
        Pair    p, q;

        // The left-hand-side is a reference type so the right-hand-side 
        // must be (and is) a reference
        p = new Pair();

        p.a = 10; // The left-hand-side is a primitive type
        p.b = 20; // The left-hand-side is a primitive type
        
        
        // The memory address on the right-hand-side is assigned to the 
        // reference type variable on the left-hand-side
        //
        q = p;

        q.a = 99; // Change the value of the attribute a in the Pair
                  // that is referred to by q
        
        System.out.println(p.a);
        
Copies of an Object
Aliases, Shallow Copies and Deep Copies
    public class Customer
    {
        public int     id;
    }

    public class Account
    {
        public Customer  holder;
        public double    balance;
    }
    
       Account      a, d, s, o;
       Customer     wilma;

       // Instantiate the Customer
       wilma      = new Customer();
       wilma.id   = 1;
       
       // Instantiate the original Account object
       o         = new Account();
       o.holder  = wilma;
       o.balance = 100.00;

       // a is an alias for o
       a = o;

       // s is a shallow copy of o
       s = new Account();
       s.holder  = o.holder;
       s.balance = o.balance;

       // d is a deep copy of o
       d = new Account();
       d.holder      = new Customer();
       d.holder.id   = o.holder.id;
       d.balance     = o.balance;
    
Comparing Reference Types

The relational equals operator (i.e., ==) can be used with reference types, and it is sometimes necessary to do so, but you must understand that it compares two references.

javaexamples/basics/AkaExample2.java (Fragment: 0)
        Color    a, b, c;

        a = new Color(153, 0, 102);
        b = a; 
        c = new Color(153, 0, 102);
        
        
        // Compare the references a, b, and c
        //
        if (a == b) System.out.println("a and b are aliases");
        if (a == c) System.out.println("a and c are aliases");
        if (b == c) System.out.println("b and c are aliases");
        
Comparing Reference Types (cont.)
javaexamples/basics/AkaExample2.java (Fragment: 1)
        // Compare the R,G,B values in a, b, and c
        //
        if (a.equals(b)) System.out.println("a and b have the same RGB values");
        if (a.equals(c)) System.out.println("a and c have the same RGB values");
        if (b.equals(c)) System.out.println("b and c have the same RGB values");
        
A Note About String Literals

String literals are reference types, but the compiler can be smart and store only one instance of each (a process called "interning").

    String         s, t, u;

    s = "James Madison University";
    t = s;
    u = "James Madison University";

    // This will, not surprisingly, print "Same contents"
    if (s.equals(u)) System.out.println("Same contents");

    // This will, not surprisingly, print "Same reference"
    if (s == t) System.out.println("Same reference");

    // This may, somewhat surprisingly, print "Same reference"
    // but you shouldn't rely on it
    if (s == u) System.out.println("Same reference");
    
A Note About the "null String"

Since String objects are reference types, a String variable can contain the reference null.

    String         s;

    s = null;
    

This is not the same thing as what is sometimes called the "null String" (and sometimes, perhaps less ambiguously, called the "empty String").

    String         t;

    t = "";
    
A Note About Static Attributes

Consider the following example:

    double unitCircumference;

    unitCircumference = 2.0 * Math.PI;
    

and recall that the Math class has two static double attributes, E and PI.

A Note about Static Attributes (cont.)
images/JavaMemoryModel_StaticAttributeExample.png
Unused/Unwanted Memory
Garbage Collection
An Example Revisited: Garbage Collection

Note that the address of c changes every iteration. Hence the memory that was allocated in the previous iteration becomes garbage.

javaexamples/basics/MemoryAllocationExample4.java (Fragment: 0)
        int     i, n;                // Compile-Time
        Color   c;                   // Compile-Time


        n = 3;

        for (i=0; i < n; i++)
        {
            c = new Color(i, i, i);    // Run-Time
        }
        
A Brief Discussion of Arrays