In article <>,
Knute Johnson <> wrote:
>
>Integer x = new Integer(5);
>os.writeObject(x);
>x = new Integer(10);
>os.writeObject(x);
>
>What gets written here? The two objects are not equivalent but have the
>same reference.
No, they do not have the same reference. They have the same variable
name, but the outputstream doesn't know their names anyway so it
doesn't care.
When you write
x = new Integer(10);
then you are changing what the variable x is referencing. It used to
reference an Integer object with the value 5, but after that
assignment, it in stead references a newly created Integer object with
the value 10. Incidentally, at this point, your Integer(5) object is
not referenced by anyone anymore and so will get garbage collected
(eventually).
Cheers
Bent D
--
Bent Dalager -
-
http://www.pvv.org/~bcd
powered by emacs