<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: C++ passes by reference, Java and Ruby don&#8217;t</title>
	<atom:link href="http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/</link>
	<description>What web development means....</description>
	<lastBuildDate>Tue, 13 Jul 2010 05:39:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Valera10</title>
		<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/comment-page-1/#comment-8780</link>
		<dc:creator>Valera10</dc:creator>
		<pubDate>Sat, 08 May 2010 11:36:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316#comment-8780</guid>
		<description>&lt;a href=&quot;http://www.lacucina.livejournal.com/&quot; rel=&quot;nofollow&quot;&gt;итальянские рецепты&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p><a href="http://www.lacucina.livejournal.com/" rel="nofollow">итальянские рецепты</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Video Technology</title>
		<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/comment-page-1/#comment-2177</link>
		<dc:creator>Video Technology</dc:creator>
		<pubDate>Thu, 28 May 2009 08:56:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316#comment-2177</guid>
		<description>Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?</description>
		<content:encoded><![CDATA[<p>Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony Williams</title>
		<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/comment-page-1/#comment-1959</link>
		<dc:creator>Anthony Williams</dc:creator>
		<pubDate>Thu, 07 May 2009 15:11:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316#comment-1959</guid>
		<description>Just because similar-looking code has different semantics in C++ and Java doesn&#039;t mean that &quot;C++ passes by reference but Java doesn&#039;t&quot;.

Java passes by reference because if you modify the properties of the passed element then the original is modified, as can be seen if you define swap as so:

 public static void swap(Point p1,Point p2){
        double tempx = p1.x;
        double tempy = p1.y;
        p1.x = p2.x;
        p1.y = p2.y;
        p2.x = tempx;
        p2.y = tempy;
    }

If you pass by value in C++ you cannot modify the original.

Object references in Java behave like pointers in C++, so you can assign the references around as much as you like (which is what your swap does) without affecting the referenced values. The difference with C++ is that C++ references (rather than pointers) cannot be reassigned, so assignment affects the referenced object rather than changing the reference.</description>
		<content:encoded><![CDATA[<p>Just because similar-looking code has different semantics in C++ and Java doesn&#8217;t mean that &#8220;C++ passes by reference but Java doesn&#8217;t&#8221;.</p>
<p>Java passes by reference because if you modify the properties of the passed element then the original is modified, as can be seen if you define swap as so:</p>
<p> public static void swap(Point p1,Point p2){<br />
        double tempx = p1.x;<br />
        double tempy = p1.y;<br />
        p1.x = p2.x;<br />
        p1.y = p2.y;<br />
        p2.x = tempx;<br />
        p2.y = tempy;<br />
    }</p>
<p>If you pass by value in C++ you cannot modify the original.</p>
<p>Object references in Java behave like pointers in C++, so you can assign the references around as much as you like (which is what your swap does) without affecting the referenced values. The difference with C++ is that C++ references (rather than pointers) cannot be reassigned, so assignment affects the referenced object rather than changing the reference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samuel Gyger</title>
		<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/comment-page-1/#comment-1761</link>
		<dc:creator>Samuel Gyger</dc:creator>
		<pubDate>Sat, 25 Apr 2009 06:20:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316#comment-1761</guid>
		<description>Oh a small mistake, there must be a star in front of p2.</description>
		<content:encoded><![CDATA[<p>Oh a small mistake, there must be a star in front of p2.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samuel Gyger</title>
		<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/comment-page-1/#comment-1760</link>
		<dc:creator>Samuel Gyger</dc:creator>
		<pubDate>Sat, 25 Apr 2009 06:20:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316#comment-1760</guid>
		<description>Thank you for this information. I remember this with the following idea.
C++
&lt;code&gt;
void swap(int* p1, int* p2){
   int* temp;
   temp = p1;
   p1 = p2;
   p2 = temp;
}

int i1 =10, i2 = 20;
int* p1,p2;
p1= &i1; p2 = &i2;

swap(p1,p2);
&lt;/code&gt;
You could try this, this would not swap the pointers in the outer code, because the pointers are local copies in the swap function.</description>
		<content:encoded><![CDATA[<p>Thank you for this information. I remember this with the following idea.<br />
C++<br />
<code><br />
void swap(int* p1, int* p2){<br />
   int* temp;<br />
   temp = p1;<br />
   p1 = p2;<br />
   p2 = temp;<br />
}</p>
<p>int i1 =10, i2 = 20;<br />
int* p1,p2;<br />
p1= &i1; p2 = &i2;</p>
<p>swap(p1,p2);<br />
</code><br />
You could try this, this would not swap the pointers in the outer code, because the pointers are local copies in the swap function.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PeterReilly</title>
		<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/comment-page-1/#comment-1747</link>
		<dc:creator>PeterReilly</dc:creator>
		<pubDate>Fri, 24 Apr 2009 22:07:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316#comment-1747</guid>
		<description>two words : copy constructor</description>
		<content:encoded><![CDATA[<p>two words : copy constructor</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: khelll</title>
		<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/comment-page-1/#comment-1732</link>
		<dc:creator>khelll</dc:creator>
		<pubDate>Fri, 24 Apr 2009 12:22:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316#comment-1732</guid>
		<description>I just was reading my rss feed and found &lt;a href=&quot;http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx&quot; rel=&quot;nofollow&quot;&gt;this&lt;/a&gt; post, seriously speaking, i hope it will help you all to get what i was trying to do in 2 consequent posts.

Good luck ;)</description>
		<content:encoded><![CDATA[<p>I just was reading my rss feed and found <a href="http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx" rel="nofollow">this</a> post, seriously speaking, i hope it will help you all to get what i was trying to do in 2 consequent posts.</p>
<p>Good luck <img src='http://www.khelll.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: khelll</title>
		<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/comment-page-1/#comment-1727</link>
		<dc:creator>khelll</dc:creator>
		<pubDate>Fri, 24 Apr 2009 11:17:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316#comment-1727</guid>
		<description>@ Artur, what you are saying regarding values is true. However I&#039;m not discussing swapping references, you can even do:
&lt;pre lang=&quot;cpp&quot;&gt;
void swap(Point &amp;p1, Point &amp;p2) {
    cout &lt;&lt; &quot;Before swap&quot; &lt;&lt; &amp;p1 &lt;&lt; endl;
    Point temp = p1;
    p1 = p2;
    p2 = temp;
    cout &lt;&lt; &quot;After swap&quot; &lt;&lt; &amp;p1 &lt;&lt; endl;
}
&lt;/pre&gt;
And you will notice that the references are the same, just the values are swapped.
I&#039;m just saying that(and if you notice the comment and java example just before your last comment), even if u made new values, the passed arguments won&#039;t change cause what is passed is the value of copied reference, not the reference itself.
I hope the idea is clear now.</description>
		<content:encoded><![CDATA[<p>@ Artur, what you are saying regarding values is true. However I&#8217;m not discussing swapping references, you can even do:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> swap<span style="color: #008000;">&#40;</span>Point <span style="color: #000040;">&amp;</span>p1, Point <span style="color: #000040;">&amp;</span>p2<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Before swap&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>p1 <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    Point temp <span style="color: #000080;">=</span> p1<span style="color: #008080;">;</span>
    p1 <span style="color: #000080;">=</span> p2<span style="color: #008080;">;</span>
    p2 <span style="color: #000080;">=</span> temp<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;After swap&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>p1 <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And you will notice that the references are the same, just the values are swapped.<br />
I&#8217;m just saying that(and if you notice the comment and java example just before your last comment), even if u made new values, the passed arguments won&#8217;t change cause what is passed is the value of copied reference, not the reference itself.<br />
I hope the idea is clear now.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Artur</title>
		<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/comment-page-1/#comment-1726</link>
		<dc:creator>Artur</dc:creator>
		<pubDate>Fri, 24 Apr 2009 10:27:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316#comment-1726</guid>
		<description>Equivalent of C++ code in java is

&lt;code&gt;
public static void swap(Point p1,Point p2){
        Point tmp = new Point(p1);
        p1.setLocation(p2);
        p2.setLocation(tmp);
}
&lt;/code&gt;

Only magic part about C++ is that it has automatic byte-wise assignement operator. When you are doing p2=p1 in c++ you are not swapping the references, you are swapping the contents of the objects. Java just lacks operator overloading to achieve the same, nothing to do with value versus reference. 
If you think it is not a difference, consider doing this with maps of million elements. According to your logic, it should be instant operation (as you are just swapping the &#039;references&#039;) - but in reality, it will copy the entire map around 3 times (not while passing to the method, but while doing = assignment).
You should compare reference to the pointer in C++ versus pointer in java - this would do pure swap.</description>
		<content:encoded><![CDATA[<p>Equivalent of C++ code in java is</p>
<p><code><br />
public static void swap(Point p1,Point p2){<br />
        Point tmp = new Point(p1);<br />
        p1.setLocation(p2);<br />
        p2.setLocation(tmp);<br />
}<br />
</code></p>
<p>Only magic part about C++ is that it has automatic byte-wise assignement operator. When you are doing p2=p1 in c++ you are not swapping the references, you are swapping the contents of the objects. Java just lacks operator overloading to achieve the same, nothing to do with value versus reference.<br />
If you think it is not a difference, consider doing this with maps of million elements. According to your logic, it should be instant operation (as you are just swapping the &#8216;references&#8217;) &#8211; but in reality, it will copy the entire map around 3 times (not while passing to the method, but while doing = assignment).<br />
You should compare reference to the pointer in C++ versus pointer in java &#8211; this would do pure swap.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: khelll</title>
		<link>http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/comment-page-1/#comment-1723</link>
		<dc:creator>khelll</dc:creator>
		<pubDate>Fri, 24 Apr 2009 09:42:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.khelll.com/blog/?p=316#comment-1723</guid>
		<description>@Artur, the problem is not in Java&#039;s &#039;=&#039; operator, otherwise I could also have the same result as c++ references if I did this in java instead of simple assignment:
&lt;pre lang=&#039;java&#039;&gt;
public double getX(){return x;}
public double getY(){return y;}

public static void swap(Point p1,Point p2){
        double x1 = p1.getX();
        double y1 = p1.getY();
        p1 = new Point(p2.getX(),p2.getY());
        p2 = new Point(x1,y1);
 }
&lt;/pre&gt;

but even doing this won&#039;t affect the passed arguments, which will keep having their original values.</description>
		<content:encoded><![CDATA[<p>@Artur, the problem is not in Java&#8217;s &#8216;=&#8217; operator, otherwise I could also have the same result as c++ references if I did this in java instead of simple assignment:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> getX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> x<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> getY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> y<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> swap<span style="color: #009900;">&#40;</span><span style="color: #003399;">Point</span> p1,<span style="color: #003399;">Point</span> p2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">double</span> x1 <span style="color: #339933;">=</span> p1.<span style="color: #006633;">getX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">double</span> y1 <span style="color: #339933;">=</span> p1.<span style="color: #006633;">getY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        p1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Point</span><span style="color: #009900;">&#40;</span>p2.<span style="color: #006633;">getX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,p2.<span style="color: #006633;">getY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        p2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Point</span><span style="color: #009900;">&#40;</span>x1,y1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p>but even doing this won&#8217;t affect the passed arguments, which will keep having their original values.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
