4

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 ?

Mateusz Chromiński
  • 2,742
  • 4
  • 28
  • 45
a Learner
  • 4,944
  • 10
  • 53
  • 89

4 Answers4

4

While performing shallow copy is it necessary to "create a new instance" ?

Yes, you must create an instance to create a copy (either shallow or deep) of your object. Just doing the assignment of reference just creates a copy of reference which points to the same instance.

You have used a non-static method that is creating a copy. But generally I prefer two ways: -

Either use a copy-constructor: -

public A(A obj) {
    copy.aValue = obj.aValue;
}

And use it like: -

A first = new A();
A copy = new A(first);

Or, use a public static method which takes an instance and returns a copy of that.

public static A createCopy(A obj) {
    A copy = new A();
    copy.aValue = obj.aValue;
    return copy;
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Why should you not use a non-static method for creating a copy? That sounds totally reasonable to me! – akuhn Dec 02 '12 at 08:23
  • 1
    @akuhn. Yeah that is reasonable. It's all user preference. I prefer `copy constructor` or `static factory`. Well, I have edited that line of me. Thanks :) – Rohit Jain Dec 02 '12 at 08:26
  • `In shallow copy, you just copy the references and not primitives`- Sounds strange!. I think While copying we make copy of all the attributes which we want. – a Learner Dec 02 '12 at 08:27
  • @aLearner.. That is why we have different types of copy. It's `Deep copy` that copies all the attributes. Not in `shallow copy`. – Rohit Jain Dec 02 '12 at 08:28
  • @aLearner. See this post - http://stackoverflow.com/questions/1175620/in-java-what-is-a-shallow-copy – Rohit Jain Dec 02 '12 at 08:30
  • @aLearner.. Sorry, I read your example wrong. You are creating a `shallow copy` in your method. Yeah, in deep copy we don't copy the values of reference,rather we create a copy of each reference. That's why I wrote there earlier. – Rohit Jain Dec 02 '12 at 08:39
  • Anyways your answer is still nice and cleared some more points. I have deleted my comment. – a Learner Dec 02 '12 at 08:40
  • @aLearner. Umm. and you can ask for more clarification, if you still have any doubts. – Rohit Jain Dec 02 '12 at 08:42
3

Assignment is not a copy - it's just a second reference to the same instance.

A copy must be a new instance, otherwise it isn't a copy; it's just another reference.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

While performing shallow copy is it necessary to "create a new instance" ? as:

Yes, it is necessary.

Or copy by assignment is also considered as shallow copy:

No, this is a totally different operation. There is no copy; this simply creates a new reference to the existing object. If you modify the object through this new reference, you are still modifying the original object.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Then it means at wiki definition of shallow copy is wrong and also at SO question `http://stackoverflow.com/questions/6182565/java-deep-copy-shallow-copy-clone` example given by `Jay Elston` is wrong. – a Learner Dec 02 '12 at 08:19
  • @aLearner: I think you're misreading the materials you refer to. – NPE Dec 02 '12 at 08:21
0

Reference assignment doesn't create a new object but just points to existing object.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55