However, the situation is not as straightforward as that, and this article explains why! Java does pass by reference, but the reference inside a method is a copy of the reference in the calling method. Thus changes to the object referenced by the reference will "stick," but try to swap the objects inside a method and nothing is retained outside of the method! It's because only the references are swapped, but the references are only copies of the original references; thus the original references are unchanged.
Thursday, August 12, 2010
Java: Pass by Reference or Value?
However, the situation is not as straightforward as that, and this article explains why! Java does pass by reference, but the reference inside a method is a copy of the reference in the calling method. Thus changes to the object referenced by the reference will "stick," but try to swap the objects inside a method and nothing is retained outside of the method! It's because only the references are swapped, but the references are only copies of the original references; thus the original references are unchanged.
Labels:
Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Please avoid the use of profanity and be as helpful as you can!