An in-depth look into the "reference" concept in Computer Science
 

  • In CS170/CS171, you have studied the refercence concept

  • In CS255, we will use a lot of references

  • Therefore:

      • I want to make sure that you understand the (program) reference in very well before we move on.

Reference variable in Java
 

In CS170, you have learned that a reference variable is defined as follows:

                         
 BankAccount  Mary;   // Mary is a reference variable
 String       s;      // s is a reference variable
  

What exactly is a reference variable ???

What is a reference in GENERAL ?
 

  • The word reference is derived from the verb:

      • refer to which means:    point to      

  • A reference is "something" that points to something else

           

References used in Computer Science

  • A reference in general will point to something

  • A reference in Computer Science will always point to a location in memory

    Example: these "pointing fingers" are pointing at 2 different memory locations

      Points at memory location 1 Points at memory location 4

How do we refer to a memory location ?

  • A computer program does not have "pointing fingers" that point to some location in memory

  • The reference (= pointer) in a computer program consists of a variable that contains the address of the memory location where the "pointing finger" is pointing at

Example: a variable containing the address 1 will point to memory location 1

How do we refer to a memory location ?

  • A computer program does not have "pointing fingers" that point to some location in memory

  • The reference (= pointer) in a computer program consists of a variable that contains the address of the memory location where the "pointing finger" is pointing at

Example: a variable containing the address 4 will point to memory location 4

Reference variable in Java - REVISITED
 

In CS170, you have learned that a reference variable is defined as follows:

                         
 BankAccount  Mary;   // Mary is a reference variable
 String       s;      // s is a reference variable
  

What exactly is a reference variable ??? - This should be clear now....

                         
 BankAccount  Mary;  
                    
 String       s;      
                   
  

 

Reference variable in Java - REVISITED
 

In CS170, you have learned that a reference variable is defined as follows:

                         
 BankAccount  Mary;   // Mary is a reference variable
 String       s;      // s is a reference variable
  

What exactly is a reference variable ??? - This should be clear now....

                         
 BankAccount  Mary;   // Mary contains the address 
                      // of a BankAccount object
 String       s;      // s contains the address 
                      // of a String object
  

 

Reference variable in Java - REVISITED

What happens inside a Java program when it executes the following statements:

  BankAccount  Mary;   // Mary is a reference variable
  Mary = new BankAccount( ); 

I will illustrates the effects of each statement with diagrams:

Reference variable in Java - REVISITED

The variable definition will create (= reserve) 4 bytes memory to store an 32 bits address:

  BankAccount  Mary;   // Mary is a reference variable
  Mary = new BankAccount( ); 

The Java compiler will remember the address of the variable associated the identifier Mary:

Reference variable in Java - REVISITED

The new BankAccount( ) expression will create (= reserve) a BankAccount object:

  BankAccount  Mary;   // Mary is a reference variable
  Mary = new BankAccount( ); 

The new BankAccount( ) expression returns the address of the new object:

Reference variable in Java - REVISITED

The assignment operator will assign (= store) the return value in memory associated with Mary:

  BankAccount  Mary;   // Mary is a reference variable
  Mary = new BankAccount( ); 

Result:   the reference variable Mary points to the new BankAccount object:

We have used a reference previously !!!
 

The value stored in the program counter (PC) is a program reference (= memory address):

 

We have used a reference previously !!!
 

The value stored in the program counter (PC) is a program reference (= memory address):

You can see that the PC is pointing at memory location 44 !!!