Pointers and references: Difference between revisions

Content added Content deleted
No edit summary
Line 328: Line 328:
}
}


Java is call-by-value with primitives and call-by-reference with objects. So it is not possible to change the value of a primitive variable of the caller that is passed in. As noted above, if the object that the reference is pointing to is mutable, then it is possible to modify that object's state through the reference, or change the reference altogether. Then if the calling function has a reference to the same object, they will see that modification through the reference. So if you want to reflect changes in an argument back to the caller, one thing that you can do is wrap the argument as a field in an object, then modify the object through the reference.
Java is call-by-value. When passing arguments, you are either passing primitives by value or references by value. There is no such thing as "passing an object" as objects are not values in the language. As noted above, if the object that the reference is pointing to is mutable, then it is possible to modify that object's state through the reference. Then if the calling function has a reference to the same object, they will see that modification through the reference. So if you want to reflect changes in an argument back to the caller, one thing that you can do is wrap the argument as a field in an object, then modify the object through the reference.


=={{header|OCaml}}==
=={{header|OCaml}}==