• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How Java pass arguments

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output of the above program is:

Variable number before any modification: 1
Variable number modified: 10
Variable number any modification: 1

Array contents before modification:
100 200 300
Array contents modified:
5600 11200 16800
Array contents after modification:
5600 11200 16800

Why is the value of original array got modified too if Java use pass-by-value?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Stuart wrote:
Why is the value of original array got modified too if Java use pass-by-value?



Because it is not a deep copy of the value. Only the reference (parameter) is copied. If you change the mutable object that the reference in pointing to. Or in this case, change the elements of the array, those changes will have an effect.

https://coderanch.com/how-to/java/CallByReferenceVsCallByValue

Henry
 
Marshal
Posts: 8428
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And maybe this example could help you to see, that Java indeed uses pass by value, as original object not getting modified. Read the link Henry gave it to you, and compare with the code below.
Output:
mercedes max speed: 220
mercedesUpgrade max speed: 270
 
John Stuart
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response guys!
 
Liutauras Vilda
Marshal
Posts: 8428
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome, we hope it helped.

And welcome to the Ranch
 
Dinner will be steamed monkey heads with a side of tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic