References
The file Person.java defines
a simple class that represents a person (a person has a name and an age). The
file References1.java contains a program that
instantiates three Person objects and then makes some modifications to the objects
and their references.
- Carefully hand-trace the References1.java program. Draw
a diagram to illustrate the three objects and the references to them and how
these change as the program executes. Your trace should also show what the
program prints.
- Now compile and run the program. Compare the results
to your trace. If there are any differences, re-trace the program to understand
what it is doing.
- Suppose the programmer meant to do a circular shift
in reassigning the three people -- that is he/she wanted to have the original
person2 object become person1, the original person3 become
person2 and the original person1 become person3. Revise
the code to make this happen.
The file References2.java
contains another program involving Person objects. This one illustrates the
difference between assignments involving objects and assignments involving primitive
values.
- Hand trace References2.java.
- Run the program to check your trace. If your trace was
incorrect, study the program more carefully to understand what it is doing.
The Scanner class may be useful here!
- Why do the three objects stay the same for all the different
assignments but the three integers do not?
Go back to lab2 index
page