• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why can a calling function see the modified value of an array after it's passed to a called method?  RSS feed

 
Wayne Volkov
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why can a calling function see the modified value of an array after it's passed to a called method? I know that Java is always pass-by-value - Java passes objects as references and those references are passed by value. And since an array is an object, I'd assume it behaves the same as an object - change of an array in a method is not visible outside the method - but it is. Can anyone explains why?

The following code shows that tiger[]'s value is changed outside of funk():

 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice observation Pepper .
The reason behind for array is it is stored in the heap .Here you are sending the reference (kind of pointer in c/ c++) of that array to the funk(). In the funk() when you are modifying the value of array (here tiger) by using the reference it is modifying the first location . But for simple integer it is not happening because the integer value is stored in the stack . So when you pass the integer value to the funk() and assigning some value to it the new bird variable will create in the stack .


Thanks ,
Satya
Capture.PNG
[Thumbnail for Capture.PNG]
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I know that Java is always pass-by-value - Java passes objects as references and those references are passed by value.


But if you pass the copy of a reference to a mutable object (and an array of positive length is a typical example to a mutable object) it can be changed through the reference.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always think of it this way...when I pass an object into a method, it's like handing someone a card with my home address on it. if that person then goes to that address, and paints my bedroom purple, when I go back and look at my house, I will see a purple bedroom.

However, if I hand them a card with my home address on it, they erase that address to write down a new address, and then they go THERE and paint THAT bedroom purple, when I go home, mine is unchanged.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can a calling function see the modified value of an array after it's passed to a called method?


Once again we see the value of using the correct words - you don't pass an array object, you pass a reference. Use the right words when thinking about programming to avoid confusion.

Bill
 
Wayne Volkov
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your help,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!