Java was designed this way on purpose after decades of experience with lower-level languages that let a callee meddle with a caller’s variables. Java code is much easier to understand because it is more encapsulated. You don’t have to concern yourself with unexpected side effects on your variables with every method call.
The following explanation may be helpful to people with experience with low-level languages such as assembler or FORTH, or for people who have poked around learning how Java byte code works. Java evaluates primitive method argument expressions and pushes them to the return stack. Java pushes argument references to Objects as 32 or 64-bit addresses to the return stack. The callee wakes up with the pushed parameters in its stack frame, where it also stores temporary/local/stack variables. Then, when the callee returns, it pops the stack discarding all the arguments. This is why, even if the callee modifies a parameter variable, it has no effect on the caller’s variables. The modification just affects the copy on callee’s stack frame.
However, changes made to Object fields will be see by the caller.
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[220.127.116.11]
|Feedback||You are visitor number 40,529.|