Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a List object get passed by reference? [duplicate]

Tags:

java

list

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?

like image 492
Kirill Kulakov Avatar asked Jul 09 '12 14:07

Kirill Kulakov


People also ask

Are lists passed by reference or value?

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.

Is Python list pass by value or reference?

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.”

Are lists passed by reference in Python?

Lists are already passed by reference, in that all Python names are references, and list objects are mutable.

Are lists always passed by reference in C#?

List<T> is a class, and all class instances are passed by reference.


1 Answers

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

  • Is Java "pass-by-reference" or "pass-by-value"?
like image 198
jmj Avatar answered Oct 22 '22 11:10

jmj