    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Java Parameter Semantics</title>
        <link>https://members.accu.org/index.php/journals/1024</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 12, #4 - Jul 2000 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c125/">124</a>
                    (22)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c125-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c125+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Java Parameter Semantics</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 July 2000 13:15:38 +01:00 or Mon, 03 July 2000 13:15:38 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
</div>
<p>Last week John put out a plea for some material on Java, and
like the overcommitting fool that I am, I said I would get
something to him soon. This short piece is the result of that.</p>
<p>One aspect of Java that frequently trips up the programmer
familiar with C++ is pass by reference / pass by value confusion.
In a nutshell, <span class="bold"><b>Java passes object references
by value</b></span>. So what exactly does that mean? Let us look at
an example or two:</p>
<pre class="programlisting">
public class Voodoo
{
  protected long m_count = 0;
  public long Count() { return m_count; }
  public void Add() { ++m_count;}
}
public class ParameterTest
{
  public static void main( String args[] )
  {
    ParameterTest pTest= new ParameterTest();
    Voodoo voo1 = new Voodoo();
    System.out.println( &quot;Voodoo count is: &quot;
                + voo1.Count() );   // 1
    pTest.Test( voo1 );
    System.out.println( &quot;Voodoo count is: &quot; 
                + voo1.Count() );   // 2
  }
  public void Test( Voodoo voo2 ){voo2.Add();}
}
</pre>
<p>Running the above test shows the output:</p>
<pre class="screen">
Voodoo count is: 0
Voodoo count is: 1
</pre>
<p>This exceptionally basic test shows us that Java passes by
reference (#).</p>
<p>Now consider if <tt class="methodname">Test()</tt> had been
implemented like this:</p>
<pre class="programlisting">
public void Test( Voodoo voo2 )
  {
    voo2 = new Voodoo();
    voo2.Add();
    System.out.println( &quot;Voodoo count is: &quot;  + voo2.Count() );   // 3
  }
</pre>
<p>Running this test gives the following output:</p>
<pre class="screen">
Voodoo count is: 0
Voodoo count is: 1
Voodoo count is: 0
</pre>
<p>In this case, <tt class="methodname">Test()</tt> reassigned a
new instance of <tt class="classname">Voodoo</tt> to the identifier
<tt class="varname">voo2</tt>. It called <tt class=
"methodname">Count()</tt> on this new object, printed out the value
of <tt class="methodname">Count()</tt> (in this case, the value is
1) then returned.</p>
<p>However, back in <tt class="methodname">main()</tt>, when we
print out the count at (2) the count is zero again.</p>
<p>Those of you familiar with C++ pass-by-reference may have
expected <tt class="varname">voo1</tt> to have produced the value 2
for the third <tt class="methodname">println</tt>, and this is
where the difference between C++ and Java becomes apparent. If you
remember, at the beginning of this article, I stated:</p>
<p><span class="bold"><b>Java passes object references by
value.</b></span></p>
<p>In Java, you never get ownership of objects, you merely get
ownership of references to objects. In <tt class=
"methodname">main()</tt> the calls to <tt class="literal">new
ParameterTest()</tt> and <tt class="literal">new Voodoo()</tt>
simply assign references to <tt class="varname">pTest</tt> and
<tt class="varname">voo1</tt> respectively.</p>
<p>The call to <tt class="methodname">Test()</tt> which passed in a
<tt class="classname">Voodoo</tt> object creates a new reference to
the object referenced by <tt class="varname">voo1</tt>. This can be
represented by the following ASCII graph:</p>
<pre class="literallayout">
voo1  &rarr;  Voodoo object abc  &larr;  voo2
</pre>
<p>This explains the &quot;references by value&quot; part. <tt class=
"varname">voo1</tt> and <tt class="varname">voo2</tt> are two
separate references to the same object. In our second
implementation of <tt class="methodname">Test()</tt> we created a
new <tt class="classname">Voodoo</tt> object and assigned a
reference to <tt class="varname">voo2</tt>. The graph now looks
like this:</p>
<pre class="literallayout">
voo1  &rarr;  Voodoo object abc
          Voodoo object xyz  &larr;  voo2
</pre>
<p>If you think this feature of Java is there just to catch out C++
programmers moving to Java, take heart that an ex-workmate of mine
who was a Java programmer writing C++ COM faced the same problems
but in reverse when dealing with pointers to pointers ;-)</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
