{"id":59,"date":"2011-10-23T11:49:31","date_gmt":"2011-10-23T09:49:31","guid":{"rendered":"http:\/\/doanduyhai.wordpress.com\/?p=59"},"modified":"2011-10-23T11:49:31","modified_gmt":"2011-10-23T09:49:31","slug":"java-pass-by-valuereference-or-copy-of-valuereference","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=59","title":{"rendered":"Java, pass by value\/reference or copy of value\/reference ?"},"content":{"rendered":"<p>This basic question is in any Java 101 courses and the answer is quite obvious. But in fact things are a little bit trickier than they seem to be.<br \/>\n<!--more--><br \/>\n&nbsp;<\/p>\n<h1>I Definition<\/h1>\n<p>First let&#8217;s clarify some words:<\/p>\n<ul>\n<li><strong>Value<\/strong>: we call by value the real &#8220;content&#8221; of a data. Basic data type can be a numeric value, a character or an array of character (String). Complex types are a combination of these basic types.We can represent the value in the main program memory portion as block of bytes representing the data content. Each block of bytes is defined by its address in <strong>hexadecimal<\/strong>.<a href=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-value1.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-64\" title=\"Java_pass_by_value-value\" src=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-value1.png\" alt=\"\" width=\"630\" height=\"223\" srcset=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-value1.png 710w, https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-value1-300x106.png 300w\" sizes=\"(max-width: 630px) 100vw, 630px\" \/><\/a><\/li>\n<\/ul>\n<ul>\n<li><strong>Reference<\/strong>: a reference is merely a value pointing to another value. For those who know the old good C language, it is just a pointer. Please note that the reference itself is usually a long value of 32-bit (or 64-bit on 64-bit platforms). <span style=\"color:#ff0000;\"><strong>The value of the reference on an object is simply the address in memory of the object being refered to<\/strong><\/span>.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-reference.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-66\" title=\"Java_pass_by_value-reference\" src=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-reference.png\" alt=\"\" width=\"630\" height=\"226\" srcset=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-reference.png 703w, https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-reference-300x108.png 300w\" sizes=\"(max-width: 630px) 100vw, 630px\" \/><\/a><\/p>\n<p><strong>Example 1<\/strong>:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nprivate int myNumber = 12345;\n\nprivate String text = &amp;quot;another&amp;quot;;\n<\/pre>\n<p>The visual representation of these variables in the program memory is:<\/p>\n<p><a href=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex1.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-72\" title=\"Java_pass_by_value-ex1\" src=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex1.png\" alt=\"\" width=\"630\" height=\"219\" srcset=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex1.png 706w, https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex1-300x105.png 300w\" sizes=\"(max-width: 630px) 100vw, 630px\" \/><\/a><\/p>\n<ul>\n<li><strong>pass by<\/strong>: when we say &#8220;pass by&#8221;, we mean parameter passing and returning for method calls.<\/li>\n<\/ul>\n<h1>II Pass by value<\/h1>\n<p>All primitive types are passed by value, meaning that when a method is called,<span style=\"color:#ff0000;\"><strong> all the primitives parameters taken from local context are copied and the copies are passed to the method<\/strong><\/span>. In that sense we said that the parameters are &#8220;<strong>passed by value<\/strong>&#8221; because it is the value of the primitives variables which is passed.<\/p>\n<p><strong>Example 2<\/strong>:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n...\nprivate void myMethod()\n{\n  int myNumber = 12345;\n\n  String myString = &amp;quot;testing&amp;quot;;\n\n  System.out.println(&amp;quot;myNumber before call = &amp;quot;+myNumber);\n\n  this.changeMyNumber(myNumber);\n\n  System.out.println(&amp;quot;myNumber after call = &amp;quot;+myNumber);\n}\n\nprivate void changeMyNumber(int justACopy)\n{\n  justACopy = 10;\n\n  System.out.println(&amp;quot;justACopy = &amp;quot;+myNumber);\n}\n...\n<\/pre>\n<p>The console output is:<br \/>\n<cite>myNumber before call = 12345<\/cite><\/p>\n<p><cite>justACopy = 10<\/cite><\/p>\n<p><cite>myNumber after call = 12345<\/cite><\/p>\n<p>The result is not surprising. To understand what happened under the hood, let&#8217;s have a look into the memory zone:<\/p>\n<p><a href=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex21.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-83\" title=\"Java_pass_by_value-ex2\" src=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex21.png\" alt=\"\" width=\"630\" height=\"352\" srcset=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex21.png 703w, https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex21-300x168.png 300w\" sizes=\"(max-width: 630px) 100vw, 630px\" \/><\/a><\/p>\n<p>When calling <em>changeMyNumber()<\/em>, the &#8220;myNumber&#8221; primitive value is copied and passed to the method as variable &#8220;justACopy&#8221;. Now, whatever value we assign to &#8220;justACopy&#8221;, it does not affect the original value of &#8220;myNumber&#8221;.<\/p>\n<p>The denomination<strong> pass-by-value<\/strong> is indeed incorrect, the proper name should be <strong>pass-by-copy-of-value <\/strong>because we do not pass the content of the variable itself but rather a copy of the content.<\/p>\n<h1>III Pass by reference<\/h1>\n<p>For non primitive variables, all parameters calls and returnings for method are done with references. When calling a method,<span style=\"color:#ff0000;\"> <strong> all the references of non-primitive parameters taken from local context are copied and the copies of these references are passed to the method<\/strong><\/span>.<\/p>\n<p>Example 3:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n...\nprivate void myMethod()\n{\n  int myNumber = 12345;\n\n  StringBuffer myString = new StringBuffer(&amp;quot;testing&amp;quot;);\n\n  System.out.println(&amp;quot;myString before call = &amp;quot;+myString.toString());\n\n  this.changeMyString(myString);\n\n  System.out.println(&amp;quot;myString after call = &amp;quot;+myString.toString());\n}\n\nprivate void changeMyString(StringBuffer justACopy)\n{\n  justACopy = new StringBuffer(&amp;quot;another String&amp;quot;);\n\n  System.out.println(&amp;quot;justACopy = &amp;quot;+justACopy.toString());\n}\n...\n<\/pre>\n<p>The console output is:<br \/>\n<cite>myString before call = testing<\/cite><\/p>\n<p><cite>justACopy = another String<br \/>\n<\/cite><\/p>\n<p><cite><\/cite><cite>myString <\/cite>after call = testing<\/p>\n<p>The result is this time surprising.\u00a0 We expect that the &#8220;myString&#8221; variable will be the newly created StringBuffer and contains &#8220;another String&#8221; but it&#8217;s not the case. Let&#8217;s see what happened under the hood:<\/p>\n<p><a href=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex32.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-89\" title=\"Java_pass_by_value-ex3\" src=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex32.png\" alt=\"\" width=\"630\" height=\"385\" srcset=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex32.png 701w, https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2011\/10\/java_pass_by_value-ex32-300x184.png 300w\" sizes=\"(max-width: 630px) 100vw, 630px\" \/><\/a><\/p>\n<p>When calling <em>changeMyString()<\/em>, the reference to &#8220;myString&#8221; (0x25a1c) is copied and passed to the method. In the\u00a0<em>changeMyString()<\/em> method context this reference is named &#8220;justACopy&#8221;. At that point we have 2 references pointing to the same memory location of the StringBuffer containing the &#8220;testing&#8221; value.<\/p>\n<p>When doing justACopy = new StringBuffer(&#8220;anotherString&#8221;); a new StringBuffer is created in memory at the address 0x42f59 and now the &#8220;justACopy&#8221; variable is pointing to this new location. In fact the old reference &#8220;myString&#8221; is still pointing to &#8220;testing&#8221;. What is changed by the method\u00a0<em>changeMyString()<\/em> is the copy of &#8220;myString&#8221;, not &#8220;myString&#8221; itself. That explains the output we got.<\/p>\n<p>Again the <strong>pass-by-reference<\/strong> naming is incorrect, the proper name should by<strong> pass-by-copy-of-reference<\/strong>.<\/p>\n<p>If we want to change the value of &#8220;myString&#8221; in the\u00a0<em>changeMyString()<\/em> method, we could have done:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nprivate void changeMyString(StringBuffer justACopy)\n{\n  justACopy.clear();\n  justACopy.append(&amp;quot;anotherString&amp;quot;);\n\n  System.out.println(&amp;quot;justACopy = &amp;quot;+justACopy.toString());\n}\n<\/pre>\n<p>Rather than creating a new StringBuffer whose lifecyle is limited to the scope of the\u00a0<em>changeMyString()<\/em> method, we could have changed the content of the original StringBuffer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This basic question is in any Java 101 courses and the answer is quite obvious. But in fact things are a little bit trickier than they seem to be.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22,6],"tags":[35],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/59"}],"collection":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":0,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}