Memory: Stack and Heap


Memory: Stack & Heap

WhiteMouse.java
WhiteMouseFamily.java

Deitel 3, 8.10


Classwork


Assume we have a class Account representing a bank account and a class Customer, representing a bank customer, which look like this

public class Account {
	public double balance;
	public Customer owner;
}

and

public class Customer {
	public String name;
	public Account acct;
}
 

And then we have the following class.  Please note that this program does not do anything useful and in some cases does silly things nobody would ever do in a real program.  It is just the shortest program I could write that contained all the things students usually struggle to follow when thinking through how java programs run.

 1 package memory;
 2 public class BankTrace {
 3 
 4     public static void method2(Customer cust1, Customer cust2) {
 5         cust2 = cust1.acct.owner;
 6         cust1.acct = new Account();
 7         cust2.acct.balance = 3999.50;
 8         cust2 = new Customer();
 9         cust2.acct = new Account();
10         cust2.acct.balance = 77.0;
11     }
12 
13     public static void method1(Customer cust, Account money, double balance) {
14         money.balance = balance;
15         balance = 99.33;
16         money.owner = cust;
17         cust.acct = money;
18         Account newAct = new Account();
19         newAct.balance = 22.44;
20         method2(cust, newAct.owner);
21     }
22 
23     public static void main(String[] args) {
24         Customer customerJoan = new Customer();
25         Account bankAccount = new Account();
26         double x = 4403.20;
27 
28         method1(customerJoan, bankAccount, x);
29         System.out.println("last line");
30     }
31 }
32 
 

Questions (in a Word document):

For each of the following, answer based on what would have happened right before line 29 runs.  For each, also explain your answer by saying what happens in the program to cause that result: write out what happens step by step to lead to each result, referring to line numbers. Make up memory addresses for objects as they are created, and prefix them with MM.

Alternatively, you may  type/draw out in a separate document (I would accept Word, Excel, or Powerpoint) all of what happens in memory, on the stack and the heap, as your explanation for all the questions.  If you do this, make sure when a value is replaced, you indicate it was replaced (e.g. draw a line through it) while leaving the old value readable (don't erase old values).  I will also accept a picture of this hand drawn on paper if it is legible.  You will still need to submit explicit answers to each of the questions.

  1. How many customer objects total were created?
  2. How many account objects total were created?
  3. Does the Customer represented by the variable customerJoan in main end up with an account? If so, what is the final balance of customerJoan's account?
  4. What is the final balance in the Account represented by bankAccount in main?
  5. If garbage collection ran right before line 29, how many objects would have their space returned?