Assume the following Java code:
class Thing { // Instance variables Thing next; int i; String s; // Constructor public Thing(int x, String msg) { i = x; s = msg; next = null; } } public class ThingDemo { public static void main(String argv[]) { Thing thing1 = null, thing2 = null; Thing list = null, temp = null, ref = null; // code appears in numbered problems below ... } }
Assume the code fragments in the numbered problems below are executed
in the main
method in the order they appear. The questions
in each problem refer to the program state after the fragment executes (and
before the next fragement executes).
thing1 = new Thing(99, "Hi"); thing2 = thing1;
a. How many Thing objects are there?
One. There is just one new
expression (with one
call to the constructor method for the Thing class).
Both variables thing1
and thing2
refer
to the same Thing object.
b. thing1.i
99 |
c. thing1.s
"Hi" |
d. thing2.i
99 |
e. thing2.s
"Hi" |
f. thing1.i == thing2.i
true |
g. thing1.s == thing2.s
true |
||
h. thing1 == thing2
true |
Here thing1
and thing2
refer to the
same object, so of course they are equal, and so
are all their instance variables.
thing2.s = "Bye";Write the value of each of the following expressions:
a. thing1.s
"Bye" |
b. thing2.s
"Bye" |
c. thing1 == thing2
true |
Here thing1
and thing2
refer to the same
object, so changing an instance variable in thing2
also changes it in thing1
.
thing2 = new Thing(99, "Bye");
a. How many Thing objects are there?
Two. We have executed a second new
expression.
Now thing2
refers to the new object, which is
different from the object referred to by thing1
.
b. thing1.s
"Bye" |
c. thing2.s
"Bye" |
d. thing1 == thing2
false |
Now thing1 == thing2
is false
because
these two variables refer to different objects, even though the contents
of the two objects (the values of all their instance variables) are the
same.
thing2.s = "Foo";Write the value of each of the following expressions:
b. thing1.s
"Bye" |
c. thing2.s
"Foo" |
d. thing1 == thing2
false |
Now thing1
and thing2
refer to different objects,
so changing an instance variable in thing2
does not change
anything in thing1
.
list = new Thing(1, "A"); temp = new Thing(2, "B"); temp.next = list; list = temp;Write the value of each of the following expressions:
a. list.i
2 |
b. list.s
"B" |
c. list.next.i
1 |
c. list.next.s
"A" |
The first statement creates a Thing object and attaches it to
list
. The second statement creates another Thing object and the
third and fourth statements insert it at the head of the list. Now
list
refers to the newer object, and list.i
refers to its i
instance variable. Also, list.next
refers to the older object, and list.next.i
refers to its i
instance variable.
temp = new Thing(3, "C"); temp.next = list; list = temp;
This code creates another Thing object and inserts it at the head of the list.
System.out.print("list"); for (ref = list; ref != null; ref = ref.next) { System.out.print(" -> " + ref.s); } System.out.println(".");
The for
loop here traverses the list from the head to the
tail, printing out the s
instance variable in each
object. The variable ref
initially points at the head of
the list. Each time the program executes the body of the loop, it points
ref
at the next object. When the program reaches the end of the
list, ref
becomes null
and control exits from the
loop.
list -> C -> B -> A.
The object that was created last is at the head of the list, and the object that was created first is at the end of the tail.
i
in the object
at the tail of the list.
System.out.println(list.next.next.i);
You cannot use ref
to refer to the end of the list because
now ref
is null
. Any attempt to use ref
as a reference will cause the program to throw an exception.