7

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?

Shashwat
  • 2,538
  • 7
  • 37
  • 56
  • 1
    What is call-by-sharing? – John Dvorak Dec 16 '12 at 06:36
  • Java doesn't use call by reference any time. Its always call by value – Bhavik Shah Dec 16 '12 at 06:36
  • 5
    @BhavikShah and yet the value is never an object but a reference to it. – John Dvorak Dec 16 '12 at 06:37
  • 1
    [Java passes objects as references passed by value](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – Anirudh Ramanathan Dec 16 '12 at 06:37
  • Java does support references, which are like pointers in the most important respects. The difference is that you can't treat references as memory addresses by doing arithmetic on them, converting them to and from integer types and so on. – Bhavik Shah Dec 16 '12 at 06:39
  • 2
    @JanDvorak [This](http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing) is call by sharing – Shashwat Dec 16 '12 at 06:40
  • I read [this](http://blog.lckymn.com/2010/11/12/java-uses-call-by-sharing/) and getting confused now. – Shashwat Dec 16 '12 at 06:41
  • call by reference and call by sharing differs in that you cannot pass a reference to a variable, only to an object. – John Dvorak Dec 16 '12 at 06:45
  • So when we pass primitive types, it is call-by-value. And for object types, it is call-by-reference. Is it what meant by call-by-sharing? Unlike C++, it does not create a copy of the whole object. Am I right? – Shashwat Dec 16 '12 at 06:55
  • In Java, for objects too it is call by value, but it passes the reference. Perhaps, thats why its call-by-sharing. I'm not sure about this. – Shashwat Dec 16 '12 at 06:56

3 Answers3

9

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 2
    Variables for an object are references to the object in memory, and with call-by-sharing when one passes an object (a reference to the object) the reference is copied and the actual and formal parameters will both refer to the same object. The value of the object can be altered, which makes it similar to call-by-reference, but it differs from call-by-reference since the reference to the object was copied so the identity of the object cannot be changed. Hence the original reference variable will still refer to the same object even after a swap routine that uses call-by-sharing. – MarkAWard Jul 10 '14 at 06:22
  • @MrWardo - Everything you say is technically correct (according to my understanding of "call by sharing". However, if you changed "call by sharing" to "call by value" it would *also* be correct. In short, we don't need a new term. – Stephen C Jul 10 '14 at 09:33
  • I added another answer to the question to try to clarify where the very rare usage of call-by-sharing may actually mean something slightly different – MarkAWard Jul 10 '14 at 14:28
2

It's a well known fact that Java is pass-by-value (to use the more common vernacular), but a lot of confusion arises from the fact that passing an object reference to a method allows that method to modify the object in the caller's scope (i.e., they "share" the object). So, some people erroneously believe Java is pass-by-reference for non-primitives. As I understand it, saying something is "call-by-sharing" is just making it clear that the value we're passing is a reference to an object, not the object itself.

In contrast, languages like R are pass-by-value, but pass non-primitives like vectors as deep copies (technically copy-on-write as I gather), such that changes to the object made in the method's scope do not modify the vector in the caller's scope.

To answer your two questions specifically, yes, Java is "call-by-sharing" as I've come to understand it from the Wikipedia article you linked. However, I disagree that call-by-sharing and call-by-reference differ in the way you describe. One difference is you can't write a method to swap two variables with call-by-sharing.

Travis Addair
  • 427
  • 4
  • 11
  • 3
    Java isn't pass by value, because java is only handling references. It's passing the reference value by copying it. In c++ pass by value means the object is fully copied and the copy constructor is invoked. – HopefullyHelpful May 04 '17 at 11:39
0

In languages where names for an object are references to the object in memory, with call-by-sharing when one passes an object (a reference to the object) the reference is copied and the actual and formal parameters will both refer to the same object. How is this different from call-by-value or call-by-reference...

So for the very few people who use the term (ie Clu programmers) call-by-sharing is different from call-by-value because, although we do copy the actual parameter into the formal parameter, both of them are references; if we modify the object to which the formal parameter refers, the program will be able to see those changes through the actual parameter after the subroutine returns.

Also note that during a call the value of the object can be altered, which makes it similar to call-by-reference, but it differs from call-by-reference since the reference to the object was copied so the identity of the object cannot be changed. Hence the original reference variable will still refer to the same object even after a "swap" routine that uses call-by-sharing.

MarkAWard
  • 1,699
  • 2
  • 16
  • 28