Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's a way how to pass arguments to functions. Passing by reference means the called functions' parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself). Pass by value means the called functions' parameter will be a copy of the callers' passed argument. The value will be the same, but the identity - the variable - is different. Thus changes to a parameter done by the called function in one case changes the argument passed and in the other case just changes the value of the parameter in the called function (which is only a copy). In a quick hurry:</p> <ul> <li>Java only supports pass by value. Always copies arguments, even though when copying a reference to an object, the parameter in the called function will point to the same object and changes to that object will be see in the caller. Since this can be confusing, <a href="http://www.yoda.arachsys.com/java/passing.html" rel="noreferrer">here</a> is what Jon Skeet has to say about this.</li> <li>C# supports pass by value and pass by reference (keyword <code>ref</code> used at caller and called function). Jon Skeet also has a nice explanation of this <a href="http://www.yoda.arachsys.com/csharp/parameters.html" rel="noreferrer">here</a>.</li> <li>C++ supports pass by value and pass by reference (reference parameter type used at called function). You will find an explanation of this below. </li> </ul> <p>Codes</p> <p>Since my language is C++, i will use that here</p> <pre class="lang-cpp prettyprint-override"><code>// passes a pointer (called reference in java) to an integer void call_by_value(int *p) { // :1 p = NULL; } // passes an integer void call_by_value(int p) { // :2 p = 42; } // passes an integer by reference void call_by_reference(int &amp; p) { // :3 p = 42; } // this is the java style of passing references. NULL is called "null" there. void call_by_value_special(int *p) { // :4 *p = 10; // changes what p points to ("what p references" in java) // only changes the value of the parameter, but *not* of // the argument passed by the caller. thus it's pass-by-value: p = NULL; } int main() { int value = 10; int * pointer = &amp;value; call_by_value(pointer); // :1 assert(pointer == &amp;value); // pointer was copied call_by_value(value); // :2 assert(value == 10); // value was copied call_by_reference(value); // :3 assert(value == 42); // value was passed by reference call_by_value_special(pointer); // :4 // pointer was copied but what pointer references was changed. assert(value == 10 &amp;&amp; pointer == &amp;value); } </code></pre> <p>And an example in Java won't hurt:</p> <pre class="lang-java prettyprint-override"><code>class Example { int value = 0; // similar to :4 case in the c++ example static void accept_reference(Example e) { // :1 e.value++; // will change the referenced object e = null; // will only change the parameter } // similar to the :2 case in the c++ example static void accept_primitive(int v) { // :2 v++; // will only change the parameter } public static void main(String... args) { int value = 0; Example ref = new Example(); // reference // note what we pass is the reference, not the object. we can't // pass objects. The reference is copied (pass-by-value). accept_reference(ref); // :1 assert ref != null &amp;&amp; ref.value == 1; // the primitive int variable is copied accept_primitive(value); // :2 assert value == 0; } } </code></pre> <p>Wikipedia</p> <p><a href="http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_value" rel="noreferrer"><code>http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_value</code></a></p> <p><a href="http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_reference" rel="noreferrer"><code>http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_reference</code></a></p> <p>This guy pretty much nails it:</p> <p><a href="http://javadude.com/articles/passbyvalue.htm" rel="noreferrer">http://javadude.com/articles/passbyvalue.htm</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload