• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Java pass arguments  RSS feed

 
John Stuart
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.

http://www.coderanch.com/how-to/java/CallByReferenceVsCallByValue

Henry
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for the response guys!
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, we hope it helped.

And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!