I’m new too Java, but I’m very experienced with PHP. When I was writing a Java program, I noticed that the variables aren’t what I’m expecting.
All method parameters are passed by value in Java. However, since all
Objects are actually references, you’re passing the value of the
reference when you pass an object. This means if you manipulate an
object passed into a method, the manipulations will stick.
From here: What are the differences between PHP and Java?
What does this mean? Does it mean that foo=blah in Java is like $foo=&$blah in PHP?
How can I pass just the value in Java?
What it means is, given
And
The change to
barwould be visible at the callsite. This is the manipulation that sticks. The reference to the original object was passed in. The setter was invoked for that object, so the getter at the callsite would return the new value of bar.However, given
This change would not be visible at the callsite, as the
foovariable inside the method has been reassigned. The reference was merely passed by value, the variables are not actually linked or connected in any way, they simply had a value (the reference) in common. We have now overwritten the reference that one variable had inside the method, the variable at the callsite has the same reference it had before.Does that make sense?