I am confused in the following:
In C++ we can pass a parameter to a function by reference (having declared it as a pointer or reference variable) and if we modify it inside the function, the changes are reflected to the caller when the function returns.
This is not happening in java and I am not sure I understand why.
E.g. this is a method from an object X
public boolean aMethod(int id, myClass aClass)
{
//do some logic
aClass = new MyClass();
//configure argument object aClass
return true;
}
In the calling code:
//some code processing
myClass obj = null;
if(X.aMethod(2,obj))
{
obj.methodA();//start using the object
}
I use it in C++, i.e. to return a result that notifies that the function parameter can be used, but in java this does not work.
I enter the if(X.aMethod(2,obj)) branch but the obj is null. Why is it null?
Haven't I assigned a memory address from the heap using new inside the method aMethod(int id, myClass aClass)? Am I not passing the "address" of obj in the function?
I was expecting to have the obj properly constructed and usable in the calling code. Have I misunderstood something concerning memory in java?
Java is always Pass by Value and not pass by reference, we can prove it with a simple example. Let's say we have a class Balloon like below. And we have a simple program with a generic method to swap two objects, the class looks like below. When we execute above program, we get following output.
The reason is that Java object variables are simply references that point to real objects in the memory heap. Therefore, even though Java passes parameters to methods by value, if the variable points to an object reference, the real object will also be changed.
As a rule of thumb, passing by reference or pointer is typically faster than passing by value, if the amount of data passed by value is larger than the size of a pointer.
C always uses 'pass by value' to pass arguments to functions (another term is 'call by value', which means the same thing), which means the code within a function cannot alter the arguments used to call the function, even if the values are changed inside the function.
Java passes everything by value - including references.
What this means is that if you pass an object, you can modify properties of that object, and they will persist after you return, but you can't replace the object in its entirety with a completely new object, because you can't actually modify the reference - only what the reference points to.
Before your aClass =
line:
Outside the function:
obj ---> <foo>
Inside the function:
aClass ---> <foo>
After your aClass =
line:
Inside the function:
aClass ---> <bar>
Outside the function:
obj ---> <foo>
The key thing to notice here is that aClass
doesn't point to obj
- it points to <foo>
. You're not passing the address of obj
, you're passing the address of what obj
points to. Thus, when you change what aClass
points to, that doesn't touch obj
.
As an alternative way of thinking about it:
In Java,
Bar foo = new Bar();
is equivalent to C++,
Bar *foo = new Bar();
Thus when you pass foo
to a function, you're not passing the address of foo
- you're passing the address of the allocated object. Java doesn't have the &
-operator style pass-by-reference that C/C++ do.
Check out my article on this, "Java is Pass By Value, Dammit"
http://javadude.com/articles/passbyvalue.htm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With