• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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():

 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your help,
 
reply
    Bookmark Topic Watch Topic
  • New Topic