Shallow copy means a "copy" of an object with same values of their attributes whether primitive or reference values.
While performing shallow copy is it necessary to "create a new instance" ? as:
public class A {
int aValue;
B bObj;
...
public A createShallow(A a1Obj) {
A aObj = new A();
aObj.aValue = a1Obj.aValue;
aObj.bObj = a1Obj.bObj;
return aObj;
}
}
Or copy by assignment is also considered as shallow copy:
B b = new B(10);
A a = new A(1, b);
A a1 = a;
This article at wikipedia defines shallow copy as reference variables sharing same memory block. So according to this copy by assignment will also be a shallow copy.
But is not it a variables pointing to same object instead of "copy" of an Object ?