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.
This page is posted
Optional Replicator mirror
|no blog for this page||Canadian
Your face IP:[22.214.171.124]
You are visitor number|