3

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?

Community
  • 1
  • 1
Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
  • Here's an example of how it works: http://stackoverflow.com/questions/5222/accessing-post-variables-using-java-servlets – Michael Dec 29 '11 at 04:51
  • 1
    There is no "pass by reference" in Java. Java is **always** pass-by-value. http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Matt Ball Dec 29 '11 at 05:05
  • I would read [Evaluation Strategy](http://en.wikipedia.org/wiki/Evaluation_strategy). While Java only implements `call-by-value`, the *semantics* of passing object values can be understood as `call-by-object-sharing`. (Effectively: when you pass an object, you pass *that* object, not a copy.) Java entirely lacks the concept of variable references that PHP has, which are similar to but different from `call-by-reference`. –  Dec 29 '11 at 05:17

2 Answers2

5

What it means is, given

 class Foo { 
     private int bar; 

     public void setBar(int value) {
         bar = value;
     }

     public int getBar() {
         return bar;
     }
 }

And

public void doSomethingToFoo(Foo foo) {
     foo.setBar(42);
}

The change to bar would 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.

Foo foo = new Foo();
doSomethingToFoo(foo);
int bar = foo.getBar(); // gets 42

However, given

public void doSomethingElseWithFoo(Foo foo) {
     foo = new Foo(); // assigning new instance to variable
     foo.setBar(117);
}

This change would not be visible at the callsite, as the foo variable 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.

Foo foo = new Foo();
foo.setBar(7);
doSomethingElseWithFoo(foo);
int bar = foo.getBar(); // still is 7!

Does that make sense?

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
2

Anthony Pegram has explained perfectly. I would like to add a little.

You can never pass an Object by value.

In java every Object is created in heap. You have reference or a what we can say a ladder to go to the Object. In case of a function call, you pass the reference (by value) as parameter. So you have another reference or ladder going to the same Object in your function. You can go to the Object by this reference and change it. Now if you change the reference in your function like Anthony did

 foo = new Foo();

what happens, you just created a new Object in heap and you have the reference of that object.In this step, you replace your existing reference(Reference to old Object) by the reference of newly created object. So in your function, you lost the way to go to the old Object.Now if you do any change, you are changing the newly created object, not the Object of caller function.

Thanks

Ahmed Arefin
  • 137
  • 2
  • 9
  • Nice answer. Just so you know, wars have raged on SO over whether Java parameters with reference-type are pass-by-reference of objects, or pass-by-value of references. I find it easiest to avoid that whole argument unless you can't answer the question without taking a side. – Mike Samuel Dec 29 '11 at 05:53