> Thanks this helps a lot. One little thing. How do I make sure I have a
> deep copy of an int and String? Does this work?
[quoted text clipped - 5 lines]
>
> b = a; // does this copy the string?
No. `a' and `b' are references, and both now refer
to the same String object.
> d = c; // i assume this is a copy rather than d being a reference to c
Yes. `c' and `d' are primitives, not references, so
each occurrence has its own unique identity. There is no
underlying Object for an `int' (some purists think there
ought to be, that Java should have been defined differently,
but that's the way things are).
I find it helpful to think of "reference" as a primitive
type, along with char and boolean and the rest. In this
world view, `a' is not a String at all; it's a String
*reference* whose value may, from time to time, be changed
so it refers to various different String objects. Saying
"`a' is a String" is a convenient shorthand, nothing more.
In the True Understanding `a' is a variable that can store
a primitive of the "reference to String" type, and the
String objects themselves are nameless.
Bad analogy: The reference is your street adress, and
the Object is your house. You can publish your address in
any number of directories and other places without building
a new house every time you do so. You can burn one of those
listings without burning down your house. The reference and
the thing referred to are different entities.

Signature
Eric.Sosman@sun.com
Allan Bruce - 09 Mar 2004 19:41 GMT
> > Thanks this helps a lot. One little thing. How do I make sure I have a
> > deep copy of an int and String? Does this work?
[quoted text clipped - 8 lines]
> No. `a' and `b' are references, and both now refer
> to the same String object.
so to make a copy, I need to do this:
b = new String(a);
Is that correct?
Thanks
Allan
Eric Sosman - 09 Mar 2004 20:13 GMT
> > > Thanks this helps a lot. One little thing. How do I make sure I have a
> > > deep copy of an int and String? Does this work?
[quoted text clipped - 14 lines]
>
> Is that correct?
Yes. But to return to the question in my first response,
why do you want to make this second and redundant copy of
"Hello World\n"? The world will not feel twice as welcomed
because you've said the same thing twice ...

Signature
Eric.Sosman@sun.com
Allan Bruce - 09 Mar 2004 20:50 GMT
> > > > Thanks this helps a lot. One little thing. How do I make sure I have a
> > > > deep copy of an int and String? Does this work?
[quoted text clipped - 19 lines]
> "Hello World\n"? The world will not feel twice as welcomed
> because you've said the same thing twice ...
I do have my reasons. I`m developing a qualitative reasoning inference
engine and I require to generate a copy of all the variables into a state.
Perhaps I dont need the variable names to be a copy, but everything else
does need to be a deep copy.
Thanks for your help
Allan
S Manohar - 11 Mar 2004 01:13 GMT
> I do have my reasons. I`m developing a qualitative reasoning inference
> engine and I require to generate a copy of all the variables into a state.
> Perhaps I dont need the variable names to be a copy, but everything else
> does need to be a deep copy.
> Thanks for your help
> Allan
Aha! I had to solve a very similar problem last month. I implemented
Serialisable on my data classes, and simply persisted the object using
Serialisation:
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ObjectOutputStream oos =
new ObjectOutputStream( // write whole objects (recursively)
new ZipOutputStream( // and compress them (I needed to)
baos // into a byte array
)
);
oos.writeObject(myData); // process the object
byte[] persistedData = baos.toByteArray(); // and recover the data
This seems to robustly handle deep copies of all data types
automatically. Should you, for some reason, need a shallower copy (I
suppose you should not, if your data classes are properly
encapsulated), then use 'transient' on members that are not to be
persisted, and provide a void readObject(ObjectInputStream) method to
restore the value.