0

guys, Please, can you clarify something for me?

As i understand (please correct me if i am wrong), when i pass the variables to a method or class i'm passing by value, isn't it?

if it's true, then why does Java has method .clone()?

Why do i ask this question, because i am very confused...here is the code: if i pass variables using the following code and then modify them inside the dialog, the original values (outside) are also changed.

DialogChoosePayment mDialogChoosePayment = new DialogChoosePayment(mContext,  (ArrayList<Payment>) defaultValues.getPayment(), (ArrayList<Payment>) selectedValues);
                mDialogChoosePayment.show();

But, if i use the following one, then the variables values (Original variables from outside) are not changed.

DialogChoosePayment mDialogChoosePayment = new DialogChoosePayment(mContext,  (ArrayList<Payment>) defaultValues.getPayment().clone(), (ArrayList<Payment>) selectedValues.clone());
                    mDialogChoosePayment.show();

Please, explain it to a newbie =)

Vladislav Kan
  • 354
  • 1
  • 12
  • 1
    @TheLostMind, i've read this statement, but what they say and what i have experienced is different – Vladislav Kan Oct 23 '14 at 06:34
  • @VladislavKan Then you haven't read it with thought. You may think you have experienced something else, but that's a mistake on your part. – Kayaman Oct 23 '14 at 06:40
  • @VladislavKan - `clone()` doesn't clone the *reference* types. You are basically passing another instance of the `DialogChooserPayment` with the same underlying *reference types*. – TheLostMind Oct 23 '14 at 06:51

5 Answers5

1

Java passes parameters by value. There are a lot of references on this topic. The most popular example is that you cannot simply implement method swap() that accepts 2 arguments and swaps their values.

The confusion may be caused by missunderstanding that the line like:

Object x = new Object();

indeed creates object but assigns reference to it to the variable x, so x is a reference. But if the you pass x to method you pass reference by value (or value of reference if you want).

This can give yo a tip why do we need method clone(). We need it when we want to create other object similar to the first one. Similarity means that it contains the same data and depends on the clone implementation (deep or not). But this is a absolutely another discussion.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 63k reputation and you did not think this was *yet another* duplicate of the canonical question about Java being pass by reference? – Raedwald Oct 23 '14 at 06:57
  • @Raedwald, you are probably right. This is a kind of duplicate, but IMHO not exact because OP sent specific code snippets. I however respect your opinion. – AlexR Oct 23 '14 at 07:00
  • @Raedwald, may be you are right, but my question is more specific...you will understand it when you read the answer I've chosen. Thanks for noting anyway (I'm still new to all this). – Vladislav Kan Oct 23 '14 at 07:05
  • @VladislavKan nobody should have to read the answers to a question to understand the question. Edit your question to make it clear why this is not a simple duplicate if the canonical question. – Raedwald Oct 23 '14 at 07:13
1

Source: http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

Passing Primitive Data Type Arguments

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.

Passing Reference Data Type Arguments

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.

tgogos
  • 23,218
  • 20
  • 96
  • 128
  • I got this, but now could you please tell me, so in my case if i want the method to get the object and not to touch it at all, is it the best approach to use .clone()? or there is a better way? – Vladislav Kan Oct 23 '14 at 07:00
0

Java has pass-by-value and only pass-by-value. At the same time Java has pointers (officially called references); so when't your passing an object to a method you're basically passing by value the variable that references that object.

If you modify the object then the object is seen to be modified at the called site. If you modify the reference to that the object in the called method, then this is not seen at the called site.

In your second case you create new objects with .clone() and pass them. Since there are no other references only in the called site, the changes can only be seen there.

Random42
  • 8,989
  • 6
  • 55
  • 86
0

when you pass (ArrayList<Payment>) defaultValues.getPayment(), you are actually passing reference to the array list. Any modifications using that reference will be reflected back to your list.

When you pass (ArrayList<Payment>) defaultValues.getPayment().clone(), you are creating a clone of the array list and passing reference to the cloned list. Any changes will be reflected only to the cloned array list not with the original array list.

Suresh Sajja
  • 552
  • 3
  • 8
  • But then, if i do the same but with ... for example int x, why all the modifications are not reflected back to the original x? – Vladislav Kan Oct 23 '14 at 06:52
  • Java is always pass by value, but the catch here is, for objects it uses it's reference (those reference are passed by value) and for primitives it pass the value. When you pass int x, you are passing value of x not the reference to x. so any modifications are not reflected back to x – Suresh Sajja Oct 23 '14 at 06:59
0

In the first case you have copy of references to the objects. Surely you can change state of those objects.
In the second case you have copy of references too. But those references point to the new cloned objects.

briarheart
  • 1,906
  • 2
  • 19
  • 33