Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does call-by-sharing and call-by-reference differ only while multithreading?

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?

like image 414
Shashwat Avatar asked Dec 16 '12 06:12

Shashwat


People also ask

What is difference between call by reference and call by value when would you use one over the other?

In the Call by Value method, there is no modification in the original value. In the Call by Reference method, there is a modification in the original value. In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument.

Which is more efficient call by value or call by reference?

Call by Value uses extra space for formal parameters and making Call by Reference more memory efficient. Since no copies are being made in Call by Reference, it is faster than Call by Value.

What is the difference between call by value and call by reference in Java?

Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter. While Call by Reference means calling a method with a parameter as a reference. Through this, the argument reference is passed to the parameter.


Video Answer


1 Answers

I would recommend that you don't use "call by sharing" terminology. As this Wikipedia article states:

"However, the term "call by sharing" is not in common use; the terminology is inconsistent across different sources. For example, in the Java community, they say that Java is pass-by-value, whereas in the Ruby community, they say that Ruby is pass-by-reference[citation needed], even though the two languages exhibit the same semantics. Call-by-sharing implies that values in the language are based on objects rather than primitive types."

and

"Although this term has widespread usage in the Python community, identical semantics in other languages such as Java and Visual Basic are often described as call-by-value, where the value is implied to be a reference to the object."

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.


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?

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.

like image 63
Stephen C Avatar answered Sep 20 '22 05:09

Stephen C