Results 1 to 7 of 7
- 06-25-2012, 12:44 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
call by reference or call by value in java?
I am programming in Java for quite sometimes now, but I never got stuck with the problem I am facing now.
Suppose I send a list to a function like:
List list = new ArrayList<Integer>() //code 1 in some class say Class1
//assign some numbers to 'list'
Now if I call another class method:
methodCustom(list)
which is defined as
void methodCustom(List list1) //code 2 in another class say Class2
then changing list1 in methodCustom i.e in code 2 of class 2 changes list from code1 of class 1. Weird!! I cant really understand what is happening. So I guess it is sending the reference.. M I correct? if so then what should I do if I only want to send the values? is there anything like deep copying and shallow copying?
kindly give me what is best way to send the array by value.
Along with this how can I get sublist from a list with separate pointers, I dont want my original list to be changed if I change the sublist.
So what is happening here in these cases?
Case1: List sl2 = list.subList(10,20);
Case2: List sl2 = new ArrayList<Integer>(list.subList(10,20));
Case3: List sl2 = new ArrayList(list.subList(10,20));
so can I copy array like:
for using it as call by value:
list=new ArrayList<Integer>(listPrev):
for using call by reference:
list=listPrev;
??Last edited by proggrammer; 06-25-2012 at 12:52 AM.
- 06-25-2012, 01:34 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,543
- Rep Power
- 11
Re: call by reference or call by value in java?
What do you mean by "changing"?then changing list1 in methodCustom i.e in code 2 of class 2 changes list from code1 of class 1. Weird!! I cant really understand what is happening.
(1) No amount of assigning new values to the variable list1 in the method that is called will ever change the value of the variable list in the method that did the calling. That's because Java is pass by value. Always. (And because the passing semantics is all about variables and their values).
(2) On the other hand, if the method that is called invokes methods using list1 then those methods will do whatever they do. And their effect on whatever list it is that list1's value refers to will be visible using any other variable that has the same value: ie changes to the contents of that list will be visible to the caller when the method returns. These effects concern lists and their contents, not variables and their values: that is, they have nothing to do with parameter passing semantics.
The returned sublist is said to be backed by the list. (See its API documentation). This behaviour is often useful, but if you don't want it create a new list from the returned sublist. Most List implementations (indeed most Collection implementations) offer a constructor that does precisely this: creates a new collection whose contents are those of a given collection.how can I get sublist from a list with separate pointers, I dont want my original list to be changed if I change the sublist.
I think the example you give at the end is intended to illustrate the distinction between the returned sublist and a new list created from its contents. This has nothing to do with parameter passing semantics (see above), rather, the essential difference is that adding and removing elements from the returned sublist will alter the contents of the original list while similar changes to a newly created list will not.Last edited by pbrockway2; 06-25-2012 at 01:39 AM.
- 06-27-2012, 10:04 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Re: call by reference or call by value in java?
OP: Changing list1 in methodCustom by calling 'list1.add(x)' or any other method that changes the list will change the value of the list variable in class 1. However, assigning a new value to list1 using 'list1 = x' will NOT change the value of the list variable in class 1. For your cases, as pbrockway said, the first one returns a list that is backed by the original list, while case 2 and case 3 create new lists by adding the objects from the sublist. Note that it does not deep-copy the objects, it only adds them to a new list. Editing the new list in case 2 or 3 will not affect the original list as it would in case 1. However, calling a method that changes the state of one of those objects will affect the objects in the old list, as they are the same objects, just referenced by a different list.
I agree with the first sentence. But java definitely passes objects by reference. If you pass a list to a method, it gives you a reference to the list, and if you add or remove objects from the list, you change the list in the code that called the method. Same with arrays, or setting values in objects, etc. If it was passing objects by value, it would copy the internal state of the parameters, which would mean adding or removing from the list within the method would not change the list in the code that called the method.
The best way to think about it IMHO is to remember that every java variable is actually a pointer to an object. Changing the object a variable/pointer refers to does not change the value of other variables/pointers that lead to the same object. However, calling a method changes the object everything points to, which would change it for everything else. If you think of it that way, you realize it is pass by value. As java doesn't have the concept of pointers, a variable passed to a method gives the method a reference to the object the variable points to, not access to the value of the variable itself (the value being the pointer it holds, not the object it references). Hence the ability to change the state of the object by calling one of its methods, but not the value of the variable by assigning the variable the method uses a new object.
EDIT: Tolls and pbrockway are right, strictly, java passes by value. But you always have to remember that that value is a reference, so it's passing a reference as a value (similar to passing a C/C++ pointer to a method) So, in light of this, I changed a few things in my post to make it less confusing.Last edited by Singing Boyo; 06-27-2012 at 12:32 PM.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-27-2012, 10:43 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: call by reference or call by value in java?
Sorry (and here we go again) but Java is pass by value.
That value is either a primitive or a reference.
If it were pass by reference the following:
would result in the reference value of someObject changing to the new Object created in the changeObject() method.Java Code:... Object someObject = new Object(); changeObject(someObject); .... void changeObject(Object anOjbect) { anOjbect = new Object(); }
Since Java does not do this then it is pass by value.Please do not ask for code as refusal often offends.
- 06-27-2012, 12:23 PM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Re: call by reference or call by value in java?
You're quite right that strictly, it passes by value. Still, I'd say it might be best to think of it as a mix of both because java values are references, so what it does is pass the value of a reference. It's especially confusing when programming in java after using C++, because it's very much like passing a pointer, which would be passing a reference to the object, as the value of a pointer. To me at least, passing a pointer to a method is passing the object by reference, even if it really passes the value of the pointer. Because java's variables are all reference, it is passing by value (the value of the pointer/reference), but if you ignore the fact that everything is a reference (or a primitive value) and think of every variable as the value of the object it refers to, it is very much like passing by reference. And that's very easy to forget in a language with no concept of a pointer.
It might be better to say it passes the variable by value, which means it is passing the object by reference. The value of the variable is the same thing as a reference to the object, and that's what gets passed to the method. Assigning the variable used in the message doesn't change the variable outside, but method calls on the object change the object for the rest of the world.Last edited by Singing Boyo; 06-27-2012 at 12:34 PM.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-27-2012, 01:56 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: call by reference or call by value in java?
Nothing "strictly" about it. It is pass by value, and the problem lies purely in people not realising that the value is the reference (also not helped by the choise of "reference" as a term rather than the more accurate "pointer").
Also note that C (with pointers) is pass by value.
The pointer is merely a value like a primitive, and a copy of the value is passed to the method, not a reference to the value (which would allow the value to be changed and reflected back on the original parameter).
So C and Java are both pass by value, and for the same reason, as Java's "reference" is simply a pointer.Please do not ask for code as refusal often offends.
- 06-27-2012, 03:00 PM #7
Re: call by reference or call by value in java?
Obligatory: JavaRanch Campfire - Cup Size: a Story About Variables
And its follow-up: JavaRanch Campfire - Pass By Value, PleaseHow to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
call c++ from java
By jalkalzrytron90 in forum New To JavaReplies: 4Last Post: 04-29-2012, 08:04 PM -
Is it Possible to call C++ from Java?
By Ampzor in forum Advanced JavaReplies: 6Last Post: 02-09-2012, 09:12 AM -
call by value and call by reference in java
By sandeepsai39 in forum New To JavaReplies: 7Last Post: 08-12-2010, 11:03 AM -
how to call dll from java ??
By Omarero in forum New To JavaReplies: 3Last Post: 11-13-2008, 05:14 AM -
how to call a dll from java
By katie in forum Advanced JavaReplies: 3Last Post: 12-10-2007, 10:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks