I don't understand this pass by value and pass by reference concept in java [duplicate]
Possible Duplicate:
What's the difference between passing by reference vs. passing b开发者_StackOverflowy 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.
Java uses only pass by value. That's it.
http://stackoverflow.com/search?q=[java]+pass+by+value
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.
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'.
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!
精彩评论