-2

What is difference between call-by value and call-by sharing ?

I was reading this answer , it says JavaScript is call –by sharing ? If it is true then why Java is call-by value not call by sharing .

Community
  • 1
  • 1
Sumeet Kumar Yadav
  • 11,912
  • 6
  • 43
  • 80
  • Who said it isn't? Did you read the Wikipedia article? It explains the difference. http://en.m.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing – Felix Kling Feb 26 '15 at 14:42
  • 2
    That answer is completely wrong. There's no such thing as "call by sharing". All parameter passing in JavaScript is by value. Object values are references in all cases, so object references are also passed by value. (*edit* - ok well I can't gainsay Barbara Liskov, but honestly nobody uses the term "call by sharing", at least not around me.) – Pointy Feb 26 '15 at 14:42
  • You are referring to the question which states "they can be both passed-by-value " ? – Peter Lawrey Feb 26 '15 at 14:43
  • 1
    Call by sharing is not really a common term and simply describes the practice to pass object references as values. It's still call by value. – Felix Kling Feb 26 '15 at 14:44
  • So if I am passing reference as a value it is call by sharing . – Sumeet Kumar Yadav Feb 26 '15 at 14:46
  • 3
    Forget that you ever heard the term "call by sharing". This is the first time I've *ever* heard it, and apparently it doesn't even have a clearly-defined meaning. – chrylis -cautiouslyoptimistic- Feb 26 '15 at 14:48
  • Yes. "Call by reference" means that a reference to the *variable's memory location* is passed. This allows the callee to change that variable. However, having an object reference only allows you to mutate the object, not the variable via which the object was passed. The object reference is the value of the variable. – Felix Kling Feb 26 '15 at 14:49
  • In this C language tutorial we will take a look at call by value and call by reference (also known as pass-by-value and pass-by-reference). These methods are different ways of passing (or calling) data to functions. Check This http://www.codingunit.com/c-tutorial-call-by-value-or-call-by-reference – Wasif Shahjahan Feb 26 '15 at 14:44
  • *Call/pass-by-X* terminology is best avoided as a whole because there are so many subtle variations in each particular language that the terms lose any traction. And actually the "call-by-sharing" term is the most meaningful of them all because it talks about the *effects* rather than the *mechanics*. – Marko Topolnik Feb 26 '15 at 15:14

2 Answers2

1

As Wikipedia states

the term "call by sharing" is not in common use; the terminology is inconsistent across different sources"

so while Java could be considered call by sharing it is not call this as the term doesn't have standard meaning.

Instead Java refers to "call by value" as the only variable types are primitives and references and these are always passed by value.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

The key distinction you must have in mind is, what constitutes the value of an expression. In

int i = 2+3;

the value of i is the number 5 itself. In

Date d = new Date();

the value of d is a reference to a Date object, not the object itself.

Once you have that cleared, Java is a strictly pass-by-value language. This means that the value of an expression is copied to a new location private to the called method. It can modify that location with no effect on the calling method.

However, in

Date d = new Date();
modify(d);

the object referred to by d is passed "by sharing": the called method gets a copy of the reference to the object, therefore it can modify the object in a way which the calling method will observe. What the called method cannot modify is the caller's reference to the object, so it cannot make the caller see another object when the method returns. Therefore

Date d = new Date();
Date d2 = d;
modify(d);
System.out.println(d2 == d);

is guaranteed to print true whatever modify does to its copy of the d reference, or to the object it refers to. (Remember that == only compares values).

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436