JMU
Lab: Questions about References and Memory Allocation


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)
  4. Briefly review the following documents:
    Note that a class may be referred to using its fully qualified name. For example, the String class may be referred to as the java.lang.String class and the Scanner class may be referred to as the java.util.Scanner class.

1. Assumptions: For all of the questions in this lab, you should assume that the first memory address used is 10000, that memory objects are allocated sequentially, that PhoneNumber objects are 12 bytes long, String objects have twice as many bytes as characters (i.e., 32 bytes for all of the email addresses), and that ContactInformation objects are 56 bytes long.
2. An Example: This part of the lab will help you understand memory allocation and reference types.
  1. Trace the execution of the following application (by-hand) using the tables provided. [Notes: (1) If a value/address changes, cross out the old value/address and then write in the new one. (2) All of the code in this question is in a single class named Q1. The breaks in the code are there only to help you with the tracing.]
    Q1.java (Fragment: a1)
    
    

    Object Memory Address
    ahPhone
    dbPhone
    nhPhone

    Memory Contents
    Address areaCode exchange extension




















    Q1.java (Fragment: a3)
    
    
  2. Check your answer above using the toString() method in the PhoneNumber class and the MemoryTracker class. (Note: This class does not provide information about physical memory locations. Instead, it provides simulated memory addresses that are easier to work with. It will assume that the first object created will be at memory address 10000, that memory is allocated sequentially, and that memory is never de-allocated.)

    Specifically, you should create, compile, and execute the following application. Then, put a check-mark next to each correct answer in the tables above and an 'X' next to each incorrect answer in the tables above.

    Q1.java (Fragment: a)
    
    
  3. How many of your answers were correct and how many were incorrect?


3. Another Example: This part of the lab will also help you understand memory allocation and reference types.
  1. Trace the execution of the following application (by-hand) using the tables provided. (Note: All of the code in this question is in a single class named Q2. The breaks in the code are there only to help you with the tracing.)
    The Heap

    Memory Contents
    Address areaCode exchange extension




















    Q2.java (Fragment: a1)
    
    
    Variables in main()

    Object Memory Address
    ahPhone
    dbPhone
    nhPhone

    Q2.java (Fragment: a5)
    
    
    Variables in tryToSwap()

    Object Memory Address
    a
    b
    temp

  2. Check your answer above using the toString() method in the PhoneNumber class and the MemoryTracker class using the following:
    Q2.java (Fragment: a)
    
    
  3. How many of your answers were correct and how many were incorrect?


4. Still Another Example: This part of the lab will also help you understand memory allocation and reference types.
  1. Trace the execution of the following application (by-hand) using the tables provided.
    The Heap

    Memory Contents
    Address areaCode exchange extension




















    Q3.java
    
    
    Variables in main()

    Object Memory Address
    ahPhone
    dbPhone
    nhPhone

    Variables in tryToSwap()

    Object Memory Address
    a
    b
    temp

  2. Check your answer above using the toString() method in the PhoneNumber class and the MemoryTracker class.
  3. How many of your answers were correct and how many were incorrect?


5. The Next to Last Example: This part of the lab will also help you understand memory allocation and reference types.
  1. Trace the execution of the following application (by-hand) using the tables provided.
    The Heap

    Memory Contents
    Address areaCode exchange extension
























    Memory Contents
    Address Characters






    Memory Contents
    Address email fax phone












    Variables in main()

    Object Memory Address
    ah
    ahEmail
    ahFax
    ahPhone
    db
    dbEmail
    dbFax
    dbPhone
    nh
    nhEmail
    nhFax
    nhPhone

    Q4.java
    
    
  2. Check your answer above using the toString() method in the ContactInformation and PhoneNumber classes, and the MemoryTracker class. Note that you will not be able to use the MemoryTracker class with String objects but you will, of course, be able to print the value of the String objects.
  3. How many of your answers were correct and how many were incorrect?


6. The Last Example: This part of the lab will also help you understand memory allocation and reference types.
  1. Trace the execution of the following application (by-hand). You must create the tables yourself.
    Q5.java
    
    
  2. Check your answer above using the toString() method in the ContactInformation and PhoneNumber classes, and the MemoryTracker class.
  3. How many of your answers were correct and how many were incorrect?


  4. Why do String objects behave differently from other objects in this example? (Hint: It does not have anything to do with the fact that the String class is part of the standard Java API or that String objects do not work with the MemoryTracker class.)


Copyright 2013