How Arguments Are Passed in Java
No matter reference type or primitive type of arguments, Java is always pass-by-value.
Each time a method is invoked, a copy for each argument is created in the stack memory and the copy version is passed to the method.
- If the original variable type is primitive, then simply, a copy of the variable is created inside the stack memory and then passed to the method.
- If the original type is not primitive, then a new copy of reference or pointer is created inside the stack memory, which points to the actual object data, and the new copy of reference is then passed to the method, (at this stage, two references are pointing to the same object data).
The reason why we say Java is always is pass-by-value is because of a new copy of primitive type or reference type is always created and then pass into the invoked method. Inside the method, it is that a duplicated copy of variables are being manipulated.