Hi ranchers,
thank you for guiding me, hope I got it now.
In arrays, the references of the objects are copied, not the elements themselves, thus the output.
I found also an easier way for me to understand this. And this goes without arrays.
If you have an object that has an object (...) also only the references are copied. So just after the cloning, you have two objects, but they don't share the sub objects (sub here as HAS-A, not necessarily as IS-A) only through references.
So the sub-objects are NOT distinct equal objects, only the references refer to the same original object(s).
I included an example where class Cowboy has a Horse that has a Saddle. If I clone a Cowboy, the other one has the same Saddle, but when I change the Saddle in the original, the clone gets his Saddle changed as well.
That's how I now understand "shallow copy".
Interesting, even if all the subclasses (Horse, Saddle) implemement clone(), there will be no copied object of them,
the code here is a bit long in the meanwhile, but:
prints
without arrays:
b before:java.awt.Point[x=100,y=100]
b after:java.awt.Point[x=100,y=100]
Now ARRAYS:
<0,0><0,1><0,2>
<1,0><1,1><1,2>
----------------------------
<0,0><0,1><0,2>
<1,0><1,1><1,2>
=================
points==other:false
points equals other:false
points [0][0] == other[0][0]:true
points [0][0] equals other[0][0]:true
other[0][0] before:java.awt.Point[x=0,y=0]
other[0][0] after:java.awt.Point[x=7,y=7]
**************************
And now Jim's:
----------------------------
cloned points
points:
<0,0><0,1><0,2>
<1,0><1,1><1,2>
other:
<0,0><0,1><0,2>
<1,0><1,1><1,2>
----------------------------
----------------------------
changed content of points[0][0]
points:
<-1,-1><0,1><0,2>
<1,0><1,1><1,2>
other:
<-1,-1><0,1><0,2>
<1,0><1,1><1,2>
----------------------------
----------------------------
set points[0][1] to <-2,-2>
points:
<-1,-1><-2,-2><0,2>
<1,0><1,1><1,2>
other:
<-1,-1><-2,-2><0,2>
<1,0><1,1><1,2>
----------------------------
----------------------------
set points[1] to empty array
points:
<-1,-1><-2,-2><0,2>
other:
<-1,-1><-2,-2><0,2>
<1,0><1,1><1,2>
----------------------------
At this point, the code becomes a bit long...
leather in clone before: RAW_HIDE
leather in clone after: FINE
I made the clone methods using covariant return types, so
Java 5.
The beginning of main() shows that the cloned objects themself are independent (points a and b).
Phew,
Bu.