Homework
Homework
Lecture 1
Consider the code on the following two pages. Answer the following questions:
1. What will be displayed by the code? Write down your answer in the table below before
continuing to the next question.
2. Use the template provided on the last page to draw the contents of the stack and the heap
memory at the point in time when the program reaches the line of code marked //Line A.
Did you find that you would like to change the answer you gave on the first question after
drawing the memory content? Make the changes now.
3. Execute the code in Visual Studio to determine if you correctly predicted what would be
displayed.
Were you correct? If not, would you like to change your drawing of the memory content?
public Main() {
Dummy d1 = new Dummy("A");
Dummy d2 = new Dummy("B");
Dummy d3 = d1;
int q = num;
d1.update("AAA");
d2.update("BBB");
d3.update("CCC");
System.out.println(d1);
System.out.println(d2);
doInt(d1.i);
doString(d2.s);
System.out.println(d1);
System.out.println(d2);
doPInt(d1);
doPString(d2);
//Line A
System.out.println(d1 == d2);
System.out.println(d2 == d3);
System.out.println(d3 == d1);
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
}
public Dummy(String s) {
this.s = s;
}
@Override
public String toString() {
return s + " (" + i + ")";
}
}
Stack Memory
Stack: Stack:
Type Name Value Type Name Value
Stack:
Type Name Value
Stack:
Type Name Value
Heap Memory
Address #1 #2 #3 #4 #5 #6 #7 #8 #9
:
Value:
Ref Count: