0

Possible Duplicate:
What's the difference between passing by reference vs. passing by value?
Is Java pass by reference?

I don't understand this pass by value and pass by reference concept in java. Can anyone explain me in lame words... after reading many articles, i am still not understanding it.

Community
  • 1
  • 1
John Cooper
  • 7,343
  • 31
  • 80
  • 100
  • 1
    Please search before asking: http://stackoverflow.com/search?q=java+pass+by+value+pass+by+reference – Bill the Lizard Jun 09 '11 at 12:37
  • Duplicate question http://stackoverflow.com/questions/40480/is-java-pass-by-reference – AjayR Jun 09 '11 at 12:38
  • 1
    If you want a more general explanation of the concept, look at http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – Jean Hominal Jun 09 '11 at 12:41
  • I don't know, Bill and Ajay, this seems more like "what's the difference between pass by reference and pass by value?" than "is Java pass by reference or pass by value?" It may still be a duplicate, but I don't think it's a duplicate of the ones you guys are referencing. I think Jean's link is a better one to stick on the "possible duplicate" part. – Shauna Jun 09 '11 at 12:42
  • 1
    The obvious reference :-) http://javadude.com/articles/passbyvalue.htm – helpermethod Jun 09 '11 at 12:59
  • @Shauna: You're probably right, but I also linked to the search in my comment above. Even if this question hadn't been a duplicate many times over, it's low quality enough that I would have closed it anyway. It's basically saying "I don't understand what I've read so far" without telling us specifically what has been read or what wasn't understood. This information is easy to find in Java tutorials and introductory texts. – Bill the Lizard Jun 09 '11 at 17:52
  • @Bill - Oh, I completely agree on your decision to close the question, just not the links (even the search, as it's still showing questions more along the lines of "is Java pass by ref") you provided. :) – Shauna Jun 09 '11 at 18:40
  • @Shauna: I manually edited in Jean's link, since it does look like an exact duplicate. Thanks for the feedback. – Bill the Lizard Jun 09 '11 at 18:43

2 Answers2

1

Java uses only pass by value. That's it.

http://stackoverflow.com/search?q=[java]+pass+by+value

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
1

I can attempt a layman's explanation of the general principle, but I don't use Java much so there may be some wrinkles.

OK so when you pass a variable into a function/ subroutine/ method, you have these 2 choices.

  1. Pass by value: your variable will be copied and 2 variables will exist independently, one inside the function scope and one in the calling scope. The former will cease to exist once the function completes, so the latter variable will not change. This is also known as 'pass by copy'.

  2. Pass by reference: the variable is not copied. All that is passed is a reference to the location of the original variable (in the calling scope). So, if the called function modifies the variable, it will persist even after the function returns to the calling scope.

Pass by reference is generally more efficient, especially for large variables. However functional design principles say that you should avoid using reference values for returning the result of a function.

Let me know if any clarification needed!

jambox
  • 584
  • 4
  • 15