If a function is called using Call-by-Reference
, then any changes made to the variable inside the function are affected immediately to the caller. And for Call-by-Sharing
, it is affected at the end of the function.
Question 1: Does Java uses Call-by-Sharing
instead of Call-by-Reference
?
Question 2: I think that Call-by-Sharing
differs from Call-by-Reference
only while multithreading. It is created only to decrease concurrent over-writing of values while it is being used in some other thread; to provide consistency. Am I right?
I would recommend that you don't use "call by sharing" terminology. As this Wikipedia article states:
and
The bottom line is that Java uses "call by sharing" ... but they don't call it that, and you probably shouldn't either if you want Java people to understand you.
No, you are not right.
"Call by sharing" really means "call by value" in the case where the value is an object reference. True "call by references" means you are (in effect) passing the address of a variable, and the called method can update the variable.