0

Clone() method in Java provide the way to clone one object into another object, so that changes made by second object won't reflect in first object. My question is that is using clone() and using pass-by-value are equivalent or there are differences in them.

codex
  • 1
  • 1

2 Answers2

1

They are not related as @Kayaman said but they are similar in behavior. The clone creates a copy in new locations in memory so any changes made by second object won't be reflected in the first object. If you use this clone as parameter in a method and this method changes it, the changes are reflected for the clone, this is pass by reference for the cloned object.

Wael
  • 1,533
  • 4
  • 20
  • 35
  • Java is strictly pass-by-value. It does not pass by reference. – Lew Bloch Feb 24 '17 at 03:45
  • I have to disagree, if you call modify(2); this is pass by value, but if you call modify(car); this is pass by reference, all objects are passed by reference in java. – Wael Feb 24 '17 at 07:33
  • Objects are not passed at all in Java. _Pointers_ to objects are passed in Java. By value. Disagree if you feel like it, but you'd be going against facts. Look it up. I'm not inventing this. How you can tell it's pass by value is that if a method reassigns a parameter, the caller does not see the change. That's how you know it isn't pass by reference. Seriously, look it up. – Lew Bloch Feb 24 '17 at 16:49
  • I just looked it up and you are right and I am right, when I said objects are passed by references I ment that the pointers to these objects are passed by value. In all cases, the idea is that if the method is able to alter the contents of the object as long as it doesn't create a new instance inside it. – Wael Feb 25 '17 at 06:35
  • "Pass by reference" has a specific meaning. It is objective and verifiable. It simply is not objectively, factually correct to describe Java's method passing as by reference. – Lew Bloch Feb 25 '17 at 16:29
1

Pass by value means you are passing a copy of the object to the called function. Since you are cloning your object and then passing to the function, it would provide the same effect and changes to the cloned object wouldn't reflect onto the original object.

However, it depends on how you clone your object. If you use shallow cloning to clone the object and the pass it onto the function and perform operation on references of object inside the cloned object, then the referenced objects might be changed and the effect would be somewhat different an undesired.

For Ex:

 class A
{
 int a;
}

and

class B implements Cloneable{
int b;
A a;
public B clone(){
B obj= new B();
obj.b=this.b;
obj.a=this.a;
}
}

in this case the clone method would copy the reference of the object a, and any changes to this object would result in changes to the actual object of o.

So, to produce the same effect as pass by value deep cloning should be used.

and to answer your question. They are DIFFERENT!!!

Aman J
  • 452
  • 4
  • 15
  • Java does not pass objects to methods, much less copies of objects. So in Java, pass by value does not mean "you are passing a copy of the object to the called function". What it _does_ mean is that a method argument contains the same value in the called routine as the caller passed. Changes to the parameter value inside the method are therefore not visible to the caller. For reference (pointer) arguments, this is the _opposite_ of deep cloning, therefore the opposite of what this answer purports. You pass a pointer to the _same_ object as the caller's, not a clone. Downvoted for being wrong. – Lew Bloch Feb 22 '17 at 20:32
  • @LewBloch never in my answer i said that the objects are passed to the methods. My only proposition was that, cloning would provide a similar result to pass by value. " passing a copy of the object to the called function" this meant to refer the cloned object, not a copy of the object. I think that you understood my answer incorrectly. – Aman J Feb 22 '17 at 20:43
  • Except in the part where I quoted you word for word saying that objects are passed to methods? – Lew Bloch Feb 22 '17 at 20:45
  • Cloning produces the opposite of passing Java references by value. – Lew Bloch Feb 22 '17 at 20:45
  • @LewBloch That first line was to explain the meaning of pass by value, not the way Java behaves. – Aman J Feb 22 '17 at 20:47
  • But it didn't explain pass by value. – Lew Bloch Feb 22 '17 at 20:58
  • @LewBloch By definition, pass by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. refer [link](http://courses.washington.edu/css342/zander/css332/passby.html) – Aman J Feb 22 '17 at 21:33
  • That's not a very precise definition. Pass-by-value means the value of the parameter is passed to the method. It isn't a copy of the value, as you and your link suggest, just the value of the value. When Java passes a "reference", actually a pointer, it passes the value of the pointer. That pointer _points to the same object the caller is pointing to_. Not a clone, deep or shallow. The OP didn't say they were passing a reference to a clone to a method. They asked if cloning is like passing by value. No, because cloning creates a new object. Passing a reference does not. Not the same. – Lew Bloch Feb 22 '17 at 23:42
  • Agree with you, thats what the last line of my answer suggests, the line you quoted was to define how pass by value should be understood, not the way java treats objects and functions – Aman J Feb 23 '17 at 06:06