8

what is the difference between pass by reference and call by reference in java ?

Jagan
  • 4,649
  • 19
  • 60
  • 70

3 Answers3

11

Java does not pass any variables by reference.

It is tempting to think of objects being passed by reference in Java -- but harmful. Variables of object type are references. When passed, they are passed by value.

In other languages, pass-by-reference and call-by-reference are the same thing.

Edit: More detail is provided in the existing stackoverflow question "Is Java pass by reference?" (Spoiler: No.)

Community
  • 1
  • 1
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • 1
    Aren't objects passed by reference? – Richard JP Le Guen Sep 07 '10 at 15:41
  • 1
    Technically they are passed by value, but their value is a reference. If you passed an Object `foo` into a method, and then said 'foo = new Object()', the calling method's variable that was passed into the method would remain unchanged. – StriplingWarrior Sep 07 '10 at 15:43
  • 1
    In java, everything is passed by value. pass by value. pass by value. It is accurate to say that java references are passed by value. This means that if you have a reference to an object, a, and you pass that reference to a method call, the reference a gets copied into b. Now b and a point to the same object. – hvgotcodes Sep 07 '10 at 15:43
  • 4
    No, an "object" variable is a variable that contains a reference to an object. When such a variable is passed as a parameter, it is passed by value. The value, in this case, is a reference. This is very different from actually passing the object by reference, because if you, for example, reassign the parameter to point to something different, that will have no effect on the passed object. That wouldn't be true if the object were actually passed by reference. – Jacob Mattison Sep 07 '10 at 15:44
  • @StriplingWarrior @hvgotcodes @JacobM - I see... so the `.` serves to de-reference as well as access a member of the object? – Richard JP Le Guen Sep 07 '10 at 15:56
  • How about array? I think it's passed by reference – Hendra Jaya Sep 07 '10 at 16:01
  • 3
    @Jagan: No passing a copy of a reference is pass by value. It's a copy! – Michael Clerx Sep 07 '10 at 16:07
  • @jancrot: The same principle applies with arrays: it's true that your variable is a reference to the real data structure, but when you pass it on you copy that reference, and nobody can redirect the reference you're still holding. – Michael Clerx Sep 07 '10 at 16:08
  • @Michael Clerx: Then pass by reference and call by reference are same...... ? If so why two different names ..... ? – Jagan Sep 07 '10 at 16:09
  • @jagan, its what i said. references are passed by value. Just because Java calls references references, doesn't mean passing a reference to a method is 'pass-by-reference'. If they called them pointers, it would not make it pass-by-pointer. Java only passed by value. – hvgotcodes Sep 07 '10 at 16:10
  • @Jagan: I don't know, I've never come across the term "call by reference" before. Sorry! – Michael Clerx Sep 07 '10 at 16:14
  • For what it's worth here's my blog post about it: http://whiteboxcomputing.com/java/references_and_integrity/ – Michael Clerx Sep 07 '10 at 16:15
  • @Michael Clerx: In c++ we have call-by-reference ....This is written in THE COMPLETE REFERENCE FOR C++ BY HERBERT SCHILDT TEXT BOOK – Jagan Sep 07 '10 at 16:17
  • @jagan, pass by reference would be the following scenario -- you do Object obj = new Object(); then you pass obj to a function. If, in the function, you did obj = new Object(), then obj in the scope where you called the function would point to the 2nd object. In java, the obj in the calling functions scope still points to the first Object. The reference in the function, which was passed by value, points to the second Object after the new Object() call – hvgotcodes Sep 07 '10 at 16:42
  • @Jagan, I've seen the term call-by-reference used and defined in a text on programming languages. I personally use "pass-by-reference," which better evokes passing a parameter. As for why we have multiple terms for the same concept, I could only speculate/conjecture/guess. :) – Andy Thomas Sep 07 '10 at 16:46
  • So call-by-reference is the same as pass-by-value? What does this answer mean? – NetherGranite Sep 21 '18 at 00:23
  • @NetherGranite - No, call-by-reference is *not* the same as pass-by-value, in any language. The OP is asking about Java, in which arguments are always passed by value. – Andy Thomas Sep 21 '18 at 13:07
9

Important concept - Java has no concept of "pass by reference". Everything in Java is passed by value. When you pass an object reference to a parameter in a method call, what you are really doing it is passing a value that points to the reference of your object.

The following URLs explain this in greater detail: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html and http://javadude.com/articles/passbyvalue.htm?repost

Apparently (as noted in comments to your question) the terms "pass by reference" and "call by reference" mean the same thing.

whaley
  • 16,075
  • 10
  • 57
  • 68
1

You asked but, "pass by reference" and "call by reference" are same thing.

If you are looking for difference between pass by reference and pass by value check answers to

Pass by reference or pass by value?

But remember, Java passes parameter by value.

http://javadude.com/articles/passbyvalue.htm

http://academic.regis.edu/dbahr/GeneralPages/IntroToProgramming/JavaPassByValue.htm

Community
  • 1
  • 1
YoK
  • 14,329
  • 4
  • 49
  • 67