Possible Duplicate:
Is Java pass-by-reference?
Does a List
object get passed by reference? In other words, if I pass an ArrayList (java.util.ArrayList)
object to a class, will it be automatically updated when I change it?
It's passed by value of reference. So modifications to the object can be seen outside the function, but assigning the variable to a new object does not change anything outside the function. It's essentially the same as passing a pointer in C, or a reference type in Java.
The two most widely known and easy to understand approaches to parameter passing amongst programming languages are pass-by-reference and pass-by-value. Unfortunately, Python is “pass-by-object-reference”, of which it is often said: “Object references are passed by value.”
Lists are already passed by reference, in that all Python names are references, and list objects are mutable.
List<T> is a class, and all class instances are passed by reference.
in other word: If I pass an ArrayList (java.util.ArrayList) object to a class, will it be automatically updated when I change it?
Yes
Does the List object passed by reference?
Value of reference would get passed
public updateList(List<String> names){ //.. }
Explanation
When you call updateList(someOtherList);
the value of someOtherList
which is a reference will copied to names
(another reference in method, bit by bit) so now both of them are referring to same instance in memory and thus it will change
See
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With