Two Laptop Bag*
The moose likes Beginning Java and the fly likes  array object reference in a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark " array object reference in a method" Watch " array object reference in a method" New topic
Author

array object reference in a method

sindura kapur
Greenhorn

Joined: Jul 14, 2008
Posts: 22
according to me the output of following code

should be 10 10 BUT ACTUALLY IT IS 0 10.can anyone explain me how and why is that output changes b but not a?


[HENRY: Added code tags]
[ July 26, 2008: Message edited by: Henry Wong ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

http://faq.javaranch.com/java/CallByReferenceVsCallByValue


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
BV Boose
Ranch Hand

Joined: Jul 26, 2008
Posts: 33
when you pass a parameter in java it's by value, for primitives you're passing a copy of the primitive. For objects you're passing a copy of the REFERENCE to the object. Changing the copy of a doesn't change a, but the copy of the reference to b still points to the actual b object. For a more consistent example make change:


Originally posted by sindura kapur:
according to me the output of following code

should be 10 10 BUT ACTUALLY IT IS 0 10.can anyone explain me how and why is that output changes b but not a?


[HENRY: Added code tags]

[ July 26, 2008: Message edited by: Henry Wong ]

[ July 27, 2008: Message edited by: gebuh Boose ]
Garrett OBrien
Greenhorn

Joined: Oct 25, 2005
Posts: 8


When I first saw this link, I got very nervous, till I read it. This whole thing of "pass by reference" vs "pass by value" is annoying because people don't understand what the terms mean. As the link states, Java is only pass by value, there is no pass by reference, although, you can simulate it by putting your variables in a class.

Garrett
 
GeeCON Prague 2014
 
subject: array object reference in a method