It is passed by value in both cases, for example Java. The difference is that both elements in your tests are objects, while in Java one of them would be primitive and the other an object. But whether something is primitive or the object is not related to passing compared to passing by reference. Passing by value compared to passing by reference is related to the fact that the called method can execute with variables in the calling context that are passed to it.
Let both languages and objects be ignored, and just look at what pass-by-value and pass-by-reference mean. I will use pseudocode in the undefined syntax B / Java / C / C ++ / C # / D:
Function Foo(arg) { arg = 6 } declare variable a a = 5 Foo(a) output a
If a is passed by value, the output is 5. If a is passed by reference (the variable reference a is pointed to Foo ), the result is 6, because Foo works with a using the variable reference.
Please note that there is a significant difference between your two tests.
In your first test, you assign a completely new value to a :
a = a + 5
You do not change the version of a passed to this method, you use this value to assign a new value to a .
In the second test, you simply modify the array :
array.pop
Not for example:
array = ...put something entirely new in `array`...
In your test, since you just change what the object reference points to, rather than changing the link, you certainly see this modification. But if you assigned a new array , this change would not be obvious in the calling context.
source share