{"id":942,"date":"2021-03-10T16:51:38","date_gmt":"2021-03-10T16:51:38","guid":{"rendered":"http:\/\/www.andreagirardi.it\/blog\/?p=942"},"modified":"2021-03-10T16:51:40","modified_gmt":"2021-03-10T16:51:40","slug":"java-pass-by-value-or-pass-by-reference","status":"publish","type":"post","link":"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/","title":{"rendered":"Java: Pass By Value or Pass By Reference"},"content":{"rendered":"\n<p>Data is shared between functions by passing parameters. Now, there are 2 ways of passing parameters:<\/p>\n\n\n\n<ul><li><strong>Passing by value<\/strong>: this method copies the value of actual parameter. The called function creates its own copy of argument value and the use it inside the code. As soon as the work is done on the copied value, the original parameter does not see any change. <\/li><li><strong>Passing by reference<\/strong>: the pass by reference method passes the parameters as a reference(address) of the original variable. The called function does not create its own copy, rather, it refers to the original values only. Hence, the changes made in the called function will be reflected in the original parameter as well.<\/li><\/ul>\n\n\n\n<p>Java follows the following rules in storing variables:<\/p>\n\n\n\n<ul><li>Local variables like primitives and object references are created on Stack memory<\/li><li>Objects are created on Heap memory<\/li><\/ul>\n\n\n\n<p class=\"has-text-align-center\"><strong>Java always passes arguments\u00a0<em>by value<\/em>, NOT by reference.<\/strong><\/p>\n\n\n\n<p>So how is it that anyone can be at all confused by this, and believe that Java is pass by reference, or think they have an example of Java acting as pass by reference? The key point is that Java\u00a0<strong>never<\/strong>\u00a0provides direct access to the values of\u00a0<em>objects themselves<\/em>, in\u00a0<em>any<\/em>\u00a0circumstances. The only access to objects is through a\u00a0<em>reference<\/em>\u00a0to that object. Because Java objects are\u00a0<em>always<\/em>\u00a0accessed through a reference, rather than directly, it is common to talk about fields and variables\u00a0<em>and method arguments<\/em>\u00a0as being\u00a0<em>objects<\/em>, when pedantically they are only\u00a0<em>references to objects<\/em>.\u00a0<strong>The confusion stems from this (strictly speaking, incorrect) change in nomenclature.<\/strong><\/p>\n\n\n\n<p>So, when calling a method:<\/p>\n\n\n\n<ul><li>For primitive arguments (<code>int<\/code>,\u00a0<code>long<\/code>, etc.), the pass by value is\u00a0<em>the actual value<\/em>\u00a0of the primitive (for example, 3)<\/li><li>For objects, the pass by value is the value of\u00a0<em>the reference to the object<\/em><\/li><\/ul>\n\n\n\n<p>So if you have\u00a0<code>doSomething(foo)<\/code>\u00a0and\u00a0<code>public void doSomething(Foo foo) { .. }<\/code>\u00a0the two Foos have copied\u00a0<em>references<\/em>\u00a0that point to the same objects.<\/p>\n\n\n\n<p>Naturally, passing by value a reference to an object looks very much like (and is indistinguishable in practice from) passing an object by reference.<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-942\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-942\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-942\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-942\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Data is shared between functions by passing parameters. Now, there are 2 ways of passing parameters: Passing by value: this method copies the value of actual parameter. The called function creates its own copy of argument value and the use it inside the code. As soon as the work is done on the copied value, [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-942\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-942\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-942\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-942\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/java-pass-by-value-or-pass-by-reference\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[14],"tags":[4],"_links":{"self":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/942"}],"collection":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/comments?post=942"}],"version-history":[{"count":1,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/942\/revisions"}],"predecessor-version":[{"id":943,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/942\/revisions\/943"}],"wp:attachment":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/media?parent=942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/categories?post=942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/tags?post=942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}