• 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

array object reference in a method

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 23951
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
http://faq.javaranch.com/java/CallByReferenceVsCallByValue
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
http://faq.javaranch.com/java/CallByReferenceVsCallByValue



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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic