<story><title>Java is Pass-by-Value</title><url>http://javadude.com/articles/passbyvalue.htm</url></story><parent_chain><item><author>lisper</author><text>This article is overly pedantic.  Java&amp;#x27;s semantics are pass-by-value only of you consider that the &amp;quot;values&amp;quot; that are being passed are pointers.  But Java doesn&amp;#x27;t have first-class pointers, it has implicit pointers.  The only thing you can do with a Java pointer &amp;quot;value&amp;quot; is to tacitly dereference it.&lt;p&gt;In fact, you can write a swap(x,y) function in Java.  It goes something like this:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    class foo { int value }
    
    void swap(foo x, foo y} {
      int temp = x.value;
      x.value = y.value;
      y.value = temp;
    }
&lt;/code&gt;&lt;/pre&gt;
(Yes, I know this isn&amp;#x27;t valid Java.  Cut me a little slack here, OK?)&lt;p&gt;&amp;quot;But that&amp;#x27;s cheating&amp;quot; you protest.  &amp;quot;That&amp;#x27;s not swapping the value of X and Y, that&amp;#x27;s swapping the value of a slot in the values of X and Y, which happen to be objects.&amp;quot;  Well, yeah.  So?  Compare that to this C code:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  struct foo { int value }
  
  void swap(struct foo x, struct foo y) {
    [same code as above]
  }
&lt;/code&gt;&lt;/pre&gt;
The effect of this function will be DIFFERENT.  Specifically, the C function will have no effect while the Java function will swap the value slots of X and Y because C lets you pass a structure as a value while Java does not.  All Java &amp;quot;values&amp;quot; are (tacit) pointers to objects [1].  Pounding your fist on the table and insisting that Java is pass-by-value because you can&amp;#x27;t write a swap(x,y) function obscures this crucial difference.&lt;p&gt;[1] OK, it also has ints and floats of various sizes.  But that&amp;#x27;s really it.</text></item></parent_chain><comment><author>colanderman</author><text>But Java &lt;i&gt;isn&amp;#x27;t&lt;/i&gt; pass-by-reference. Saying so is simply &lt;i&gt;wrong&lt;/i&gt;, for exactly the reasons you write off as &amp;quot;pedantry&amp;quot;. I see this play out all the time with co-workers who are new to Python (which has the same semantics) being very confused about why &amp;quot;numbers are passed by value but objects are passed by reference&amp;quot;.&lt;p&gt;Pedantry matters in our profession. Details have consequences.</text></comment>
<story><title>Java is Pass-by-Value</title><url>http://javadude.com/articles/passbyvalue.htm</url></story><parent_chain><item><author>lisper</author><text>This article is overly pedantic.  Java&amp;#x27;s semantics are pass-by-value only of you consider that the &amp;quot;values&amp;quot; that are being passed are pointers.  But Java doesn&amp;#x27;t have first-class pointers, it has implicit pointers.  The only thing you can do with a Java pointer &amp;quot;value&amp;quot; is to tacitly dereference it.&lt;p&gt;In fact, you can write a swap(x,y) function in Java.  It goes something like this:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    class foo { int value }
    
    void swap(foo x, foo y} {
      int temp = x.value;
      x.value = y.value;
      y.value = temp;
    }
&lt;/code&gt;&lt;/pre&gt;
(Yes, I know this isn&amp;#x27;t valid Java.  Cut me a little slack here, OK?)&lt;p&gt;&amp;quot;But that&amp;#x27;s cheating&amp;quot; you protest.  &amp;quot;That&amp;#x27;s not swapping the value of X and Y, that&amp;#x27;s swapping the value of a slot in the values of X and Y, which happen to be objects.&amp;quot;  Well, yeah.  So?  Compare that to this C code:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  struct foo { int value }
  
  void swap(struct foo x, struct foo y) {
    [same code as above]
  }
&lt;/code&gt;&lt;/pre&gt;
The effect of this function will be DIFFERENT.  Specifically, the C function will have no effect while the Java function will swap the value slots of X and Y because C lets you pass a structure as a value while Java does not.  All Java &amp;quot;values&amp;quot; are (tacit) pointers to objects [1].  Pounding your fist on the table and insisting that Java is pass-by-value because you can&amp;#x27;t write a swap(x,y) function obscures this crucial difference.&lt;p&gt;[1] OK, it also has ints and floats of various sizes.  But that&amp;#x27;s really it.</text></item></parent_chain><comment><author>masklinn</author><text>&amp;gt; This article is overly pedantic. Java&amp;#x27;s semantics are pass-by-value only of you consider that the &amp;quot;values&amp;quot; that are being passed are pointers.&lt;p&gt;Which is exactly what they are.&lt;p&gt;&amp;gt; But Java doesn&amp;#x27;t have first-class pointers, it has implicit pointers. The only thing you can do with a Java pointer &amp;quot;value&amp;quot; is to tacitly dereference it.&lt;p&gt;So? Just because you&amp;#x27;re not given direct access to the value itself doesn&amp;#x27;t change the semantics of the language. If you assume &amp;quot;pass-by&amp;quot; is strictly dichotomous and the choices are either value or reference, then Java can only be pass-by-value, its semantics simply don&amp;#x27;t allow the possibility of it being pass-by-ref.&lt;p&gt;IIRC the Python community calls it &amp;quot;pass reference by value&amp;quot;, sadly they seem to be more or less the only ones doing so. CLU called it &amp;quot;pass by sharing&amp;quot;.&lt;p&gt;&amp;gt; All Java &amp;quot;values&amp;quot; are (tacit) pointers to objects [1]. Pounding your fist on the table and insisting that Java is pass-by-value because you can&amp;#x27;t write a swap(x,y) function obscures this crucial difference.&lt;p&gt;Then again that Java&amp;#x27;s boxed types are boxed seems pretty obvious...</text></comment>