Pointers and references: Difference between revisions

Content added Content deleted
(→‎{{header|Java}}: The second half didn't make sense and semicolons aren't that cool)
Line 232: Line 232:
}
}


Java's is always call-by-value; hence, it is not possible to change the value of the variable of the caller that is passed in, whether it is a primitive or a reference. However, 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; and 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 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.


=={{header|Perl}}==
=={{header|Perl}}==