• Post Reply Bookmark Topic Watch Topic
  • New Topic

pass by reference in Java [for each loop]  RSS feed

 
Sonx Nkuks
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am not aiming to tackle this topic as i know its covering all over the net but would like to relate it with a for each



Does the object point to the list objects, in other words is the reference passed to o, will the change be reflected on each object in the list?
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You doubtless already know there ain't no such thing as "pass by reference" in Java™.

In a for-each loop, it is more confusing. The loop iterates the collection, copying each element into a temporary reference. If you assign to that temporary variable, it alters the copy, but has no effect on the original in the collection, array or whatever.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sonx Nkuks wrote:Does the object point to the list objects, in other words is the reference passed to o, will the change be reflected on each object in the list?


Yes.

But I don't see the connection to "pass by reference" (which simply, as Campbell stated, does not exist in Java). There is no hierarchy of references, so the reference you get by a foreach has the same quality as the reference still in the list.
 
Sonx Nkuks
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You doubtless already know there ain't no such thing as "pass by reference" in Java™.

In a for-each loop, it is more confusing. The loop iterates the collection, copying each element into a temporary reference. If you assign to that temporary variable, it alters the copy, but has no effect on the original in the collection, array or whatever.


So you say it has no effect? that means the objects on the list wont be changed meaning that for-each is pointless for my intentions
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The for-each loop has no effect on its Iterable or array. If you want to change the elements, try an old-fashioned for loop, or a method like Collections#fill(java.util.List, T).
 
Sonx Nkuks
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The for-each loop has no effect on its Iterable or array. If you want to change the elements, try an old-fashioned for loop, or a method like Collections#fill(java.util.List, T).


The reason i ask that Hauke's answer was Yes to my question? Anyway thanks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just try it out and see what happens?

Java has no pass-by-reference, only pass-by-value. However, variables of non-primitive types are references, and these can also be passed by value. Note that passing a reference by value is not the same as passing by reference.
 
Sonx Nkuks
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Why don't you just try it out and see what happens?

Java has no pass-by-reference, only pass-by-value. However, variables of non-primitive types are references, and these can also be passed by value. Note that passing a reference by value is not the same as passing by reference.


Mhh just tested this and yes the 'o' receives a reference to objects on the list thus changing its properties does affect the list. But, it seems that all objects in the list are affected by same change. So if i do o.setName("john") and the list has three object on it, all those objects will be changed to john. So a for each won't be used effectively to iterate or traverse the list to change each object with different data.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sonx Nkuks wrote:Am not aiming to tackle this topic as i know its covering all over the net but would like to relate it with a for each



Does the object point to the list objects, in other words is the reference passed to o, will the change be reflected on each object in the list?


The change o.name = "John" will be reflected in the "original" object. The foreach loop gives you references to each object in the list. As there is no difference between the references in the list and the one set by the foreach loop they will both change the same object.

You have to references to the same object. All the "normal" rules for having two references apply.

If you change the reference (variable value) itself within the foreach loop, that will have no effect to the reference in the list.



I still fail to see where this relates to "pass by reference". The foreach loop just creates new references, one by one. They are stored in the variable called o. If, on an abstract level, the foreach code block is seen as an anonymous method and "Obj o : myObjList" as a "signature" with a formal parameter of "Obj o" that is automatically set with each list element as actual parameter, then the parameter passing would be "call by value", as always in Java. But that would be very far stretching, I think.



("Obj" is not a very good name for a class.)
 
Rajagopal Mani
Ranch Hand
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sonx Nkuks wrote:So a for each won't be used effectively to iterate or traverse the list to change each object with different data.

List<Obj> myObjList = initializeObjeList();
for(Obj o : myObjList )
o.name = "John";


dynamic data instead of static(colored in red) may be one of the solution.
 
Joshua Barrett
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to jump in here but reading all that I'm now curious.
Does the reference for o point to the original object or does it point to a copy of the original object....
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua: It points to the original object. There are no copies of objects being made when you loop over a collection like that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!