Primitive
When you pass a primitive value, you pass a copy of the value.
In your case, you pass a copy of the number one. If the called method sets the value to ten, the original variable is untouched.
Imagine a space agency building a probe. They actually build two physical probes, exact copies of each other. One copy is given to the mission control team to study in case of questions or problems. The other copy is given to the launch team, and sent to some planet. When something damages the extraterrestrial probe, the other copy sitting in a lab on Earth remains untouched. This is how passing a copy of a primitive works.
Object
When you pass an object, you are actually passing a copy of the object reference, a pointer, basically the memory address where the object lives. So the calling method and the called method both share a copy of the same reference (memory address), meaning both are referencing the same single object in memory. When the called method uses that reference to alter the state of that object, both the calling and called methods perceive this change.
In your case you passed a reference to an existing StringBuilder
object. The called method alters ("mutates") that same StringBuilder
by appending more text to its contents. The calling method is accessing the same StringBuilder
object, so the calling method will perceive such a mutation of the text.
Imagine passing your home’s address to a hired plumber and to a hired painter. Both workers use that address to find, and alter, the same single house. That is how passing a copy of an object reference works.
This topic has been addressed many times on Stack Overflow. Search for “pass by value” and “pass by reference”.