Recursive pass in object by reference? JAVA - java

So I have an algorithm that forces me to pass in an object as a parameter recursively and on different depths of the recursion it sets the values of the object. The problem is Java isn't allowing me to do this because they pass in by value or something else that is just messing me up. How can you make sure that the object passed in retains the value set?

You never pass an object as an argument in Java. You either pass a reference or a primitive value. The argument is always passed by value. Java doesn't have pass-by-reference at all.
If you're passing in a reference to an object, and you want to make sure the code you call doesn't change the data within that object, your options are:
Create a copy and pass in a reference to that instead
Make your type immutable in the first place.
EDIT: Just to make it clear, if you have:
Foo f = new Foo();
then the value of f is not a Foo object. It's a reference to a Foo object. If you now call:
doSomething(f);
then the value of f is copied as the original value of the parameter within doSomething. That behaviour is basically the definition of pass-by-value. There's nothing doSomething can to do change the value of f - it will still refer to the same object afterwards. The data within that object may have changed, but it won't be a different object.
In a real-world analogy: if I give you a copy of my address, you can go and paint the front door, but you can't change where I live.

As Jon states, all non-primitives are passed by passing a copy of the reference. You don't have a choice. So your recursive method should be able to update the object if it is mutable. HOWEVER, you can't reassign a reference and expect it to propagate back up the call stack. And remember that Strings are not mutable.
In other words, if you do the following, you have not changed the object that was passed:
void myMethod(Object o){
o = new Object();
}
The above changes the local reference to o but does not change the caller's reference to o. If you need to do something like this you would need to return o.

Related

How to properly copy objects [duplicate]

This question already has answers here:
How do I copy an object in Java?
(23 answers)
Closed 8 years ago.
This might have been asked already but since I am not so sure how to phrase it I could not find it.
Essentially
Suppose we have the class a, (see code below), and we want to copy an instance of it, a1, to another instance a2.
So, in my main I would have a1.copy(a2)
I know that using copy2 method this will work. However copy1 will not. I just would like to clarify why this is. Is it because the parameter is just a "copy" of the object, so the object itself (a2) is not altered.
class a {
private int val;
public class(int val){
this.val = val;
}
public void copy1(a obj){
obj = this;
}
public void copy2(a obj) {
obj.val = this.val;
}
}
The key to understand your problem is that Java's method call is always pass-by-value, not by reference.
When you call a.copy1(b), Java copies a value of b's reference(say it is called b_copy, please note that b_copy points to the same memory location as b), and then pass b_copy to the method copy1.
And in your method of copy1, Java only changes the reference of b_copy.
public void copy1(b_copy){
b_copy=a;
}
So now :
b_copy: b_copy=a;
b: b does not change at all;
When the method ends,b_copy dies. So nothing changes on b!
While a.copy2(b) manipulates on the object itself, but it still copies a new value of b(say b_copy again) and pass into copy2
public void copy2(b_copy){
b_copy.val = a.val;
}
Since b_copy points to the same memory of b, so when you do changes on b_copy.val, you also does the same on b.val itself. That's the reason why b changes.
So now :
b_copy: b_copy, but b_copy's val changes
b: b's val also changes, since b_copy is points to the b's memory location;
And then when method ends, b_copy dies, and b has changed!
You may find more discussion on Is Java "pass-by-reference" or "pass-by-value"?
And you may also need How do I copy an object in Java?
When you call the method copy1(), the parameter is a copy of the reference of the object a obj. When you set it to this (obj = this;) the copy of the reference of the object is replaced by the current object, but the original object and the reference to the object at the place you are calling the copy1() method stays the same. You just change the copy of the reference of the object.
When you call the method copy2(), no reference work is done there, and you are doing one by one matching of the variables (properties of the objects) and there is no copy of reference work there. You are changing the object itself. In this case, this.val = obj.val; would also work.
For more understanding, check this topic and check how other languages like C or C++ handle parameter passing issue.
I think you might want to look into how parameters are passed in Java.
For primitive variables, they are passed by value. If I recall correctly, this means that a temporary value is passed and any changes to the variable in the function update only the temporary variable.
However, objects are passed by reference. This means that instead of the entire object, essentially a pointer to where the object is in memory is passed.
What this means is that updating an object's variables produces lasting effects on that object. Additionally, if you set one object equal to another object, you're really just setting the memory references to be the same (a byproduct of this is that the values of the variables in the objects are then linked).
This explains why the first function fails to do what you want (you're setting the memory locations to be the same). And the second succeeds (you're performing a deep copy into a new memory address).
Your reasoning is correct. When you pass an object to a method (as you do in copy1), all you are doing is creating another reference to the same object. If you override this reference, as copy1 does, it just means that that reference now points to a different object - it does not, and can not, change the original object.

Will the changes get reflected in the original variable?

In the following image, p points to person.
p is passed as a reference to a function named callFunction. Now,if I make any change in p will they also get reflected in person ? Please explain.
I understand that argument passed is the reference value of the original variable (I hope so !). But I can't think further.
If you change the details of p as such, it'll get reflected
// Nothing done to p before this
p.changeName("Not Sanika Anymore"); // This will be reflected as both point to the same object as references are the same
If you create a new Person and assign it to p then it wont
// New Person for p
p = new Person("New Sanika"); // now this p is pointing to the newly created object's reference
p.changeName("Not Sanika Anymore"); // This won't be reflected
That is because java is purely pass-by-value. You are passing the reference of the object as a value to callFunction().
This answer by Eng.Fouad explains this concept in a really good way.
Yes, because both reference are pointing to same Person object. When you change some attribute using any reference, same object will be updated.
As long as you change the members of the object which is pointed by the reference p , They'll get reflected , As java passes parameter by value .
What is Pass by value ??
The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. That location is typically a chunk of memory on the run-time stack for the application (which is how Java handles it), but other languages could choose parameter storage differently. So there is no way of the method having contact with the original reference here.
So Pass by reference is implemented differently , this is where actually you can have freedom of playing with reference . Here references can be changed in the method .

Java: Passing a reference to a variable as an argument for a method

When passing an object as an argument for a method, all changes that happen to the argument inside the method, affect the "original" object as well. That's because the argument is a reference to the object.
But I also want to do the same with variables- I want all changes that happen inside the method to affect the "original" variable. But I don't know how. I want to do this, because some times more than one local variables need to be processed in the same way.
How can I pass a reference to a variable as an argument for a method?
There is no other choice than to store the variable inside an object, and to pass the object. All arguments are passed by value in Java.
The object can be this (i.e. the variable is in fact a field of the current object).
Can you clarify what you mean by the word "variable"? Do you mean a local reference inside the method, a primitive that's passed in, or something else?
Java references and primitives are passed by value. You cannot alter them, no matter how much you may want to.

Java reference-type in parameter

Why doesn't this change the initial object:
public class Foo
{
Bar item = new Item(5);
public changeBar(Bar test){
test = new Item(4);
}
public run(){
changeBar(item);
}
}
In this case item doesn't get changed. How is this possible? I mean, you're passing the reference as parameter in the method, so I'd say assigning a new value to it, means the initial item would also be changed.
Can someone explain to me how this works.
=======================================
however, my question is the following:
the following does work right?
public changeBar2(Bar test){
test.parameter = "newValue";
}
I don't see how that's different.
Consider a thought experiment:
changeBar(null);
Would your code above change the value of null?
The answer to your question is that although your Bar object is passed "by reference", this is different from passing the reference itself by value. In Java, all parameters are passed by value, even when they are themselves references to other objects.
public changeBar(Bar test){
test = new Item(4);
}
changeBar(item);
Here the value in item in copied to test. So as of now, both item, test are pointing to same object(s). Inside the method, test is pointing to a different location which doesn't affect the item. You are passing by value and not by reference.
public changeBar2(Bar test){
test.parameter = "newValue";
}
The above modifies the object that is passed because both the test and item are referring to the same object.
So, test can totally either point to a new object or modify the object was earlier referring to. Both are different and are valid.
Parameters in Java are ALWAYS passed-by-value.
At the beginnning of function changeBar() test is a reference to item, but then you overwrite it with a reference to a newly created item. You therefore only overwrite the local variable with a different reference, but don't change the referenced by object.
test in changeBar is basically as pointer. So when you assign it, you are changing the pointer.
Here is an article: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
You are giving the method the reference to this object, that is, its address so to speak. So this address is stocked in a variable named test.
Then your method erases what is written in the test variable, and writes instead the address of a new Item object it created. Nothing here changes anything to the initial Item object, nor to the references to it that are outside the method's scope.
I guess I am pretty approximative here and I will be glad to get corrected.

How to pass mutable objects by value to java

Is there any way that I can pass mutable Objects by value to a function in java?
What I actually want is to pass an object to a method, do some operations on it (change it) and again call that method with that old object only(not the changed value).
here is some sample:
{ MyObj obj = new MyObj(); obj.setName("name");
append(obj);
System.out.println(obj.name);
prepend(obj);
System.out.println(obj.name);
}
void append(MyObj obj){ obj.name+="1"; }
void prepend(MyObj obj){ String a = "1"; obj.name=a+obj.name; }
At the end of this code, I want output as:
name1
1name
Objects themselves aren't passed at all in Java. Ever.
But everything is passed by value - where the only things that can be passed are primitive values and references.
It's not quite clear what you're trying to do - is the idea that you'd like to have a method with (say) a StringBuilder parameter, but without any changes made to the object from within the method being visible to the caller? If so, you basically need to clone the object yourself.
Unfortunately, no. Java never passes Objects by value, it passes the reference of the object by value.
Explanation from here:
What's really happening is that
objects are always held by reference
in java -- never by value -- and the
references are, indeed, being passed
by value.
Why do you need to do this? If you don't change the object, then it doesn't matter. If you do change the object, and don't want to affect the caller's object, then just make a copy locally. But I would guess that at least 90% of the time people think they need to do that, they really don't.
Show some code. What are you really trying to do?
AFAIK immutable/mutable is not related with passing by value/reference. Strings are passed by reference, not value. What makes string immutable is design of string class itself.
Perhaps you may explain a bit more what you looking for.

Categories

Resources