Tricky Targets Exercise
Purpose:
A reality check on the Tricky program in the lecture notes
(i.e. are the instructors telling the truth?).
It is worthwhile to
verify that the Java compiler identifies the targets that the memory
model says it should.
Download the
zip file
with all the java files and unzip it. Or copy all the
following files into a new directory:
Exercises:
-
Trace the execution of the main method (using the
memory model presented in class). Explain what output you
expect. Imagine commenting out any statement which you think
will cause compilation errors, and continue
with your tracing. Read the comments in the code regarding
the style rule for referencing static members and the use
of references of the form this.mem and super.mem.
-
Verify your tracing.
The output from running this code is out.txt.
Compare this with the results of your tracing.
- Build and run the code. It should compile and
run without error.
- Check that the output of the program agrees
with ours in out.txt.
- Experiment with the code.
If you remove the widening cast on line 45 of Tricky.java
and rerun the program,
you find the output changes from before.
Explain why.
In the distributed source code, several groups of lines have
been commented out.
Try removing some of these comments. Do you understand why the program
no longer compiles, or if it does compile, can you explain the
new output?
Try other variations that you are curious about. It is very
easy to edit, build and rerun...
- Use the DrJava debugger
to step through the execution of this code and
verify the memory model.