{"id":219,"date":"2025-06-24T07:13:16","date_gmt":"2025-06-24T07:13:16","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=219"},"modified":"2025-07-16T10:27:35","modified_gmt":"2025-07-16T10:27:35","slug":"what-is-call-by-reference-and-call-by-value","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/what-is-call-by-reference-and-call-by-value\/","title":{"rendered":"What is call by reference and call by value?"},"content":{"rendered":"\n<p>\ud83d\udce4 Call by Value (Java uses this)<\/p>\n\n\n\n<p>When a method is called, <strong>a copy of the variable&#8217;s value<\/strong> is passed. So, any changes made inside the method <strong>do not affect the original variable<\/strong>.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"825\" height=\"199\" src=\"https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-6.png\" alt=\"\" class=\"wp-image-220\" srcset=\"https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-6.png 825w, https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-6-300x72.png 300w, https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-6-768x185.png 768w\" sizes=\"auto, (max-width: 825px) 100vw, 825px\" \/><\/figure>\n\n\n\n<p>Even though <code>x<\/code> was changed inside the method, the original variable <code>a<\/code> remains unchanged.<\/p>\n\n\n\n<p>\ud83d\udd01 Call by Reference (Java <em>mimics<\/em> this with objects)<\/p>\n\n\n\n<p>Here, a <strong>reference (or address) to the actual variable is passed<\/strong>, so changes made inside the method <strong>do affect the original object<\/strong>.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"220\" src=\"https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-7.png\" alt=\"\" class=\"wp-image-221\" srcset=\"https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-7.png 864w, https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-7-300x76.png 300w, https:\/\/thetestdata.com\/blog\/wp-content\/uploads\/2025\/06\/image-7-768x196.png 768w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/figure>\n\n\n\n<p>The array\u2019s first element changed! That\u2019s because the method worked on the actual array through its reference.<\/p>\n\n\n\n<p>\ud83e\udde0 Quick Summary:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Call by value<\/strong>: You give someone a <strong>copy<\/strong> of your house key (primitives).<\/li>\n\n\n\n<li><strong>Call by reference<\/strong>: You give someone the <strong>actual key<\/strong> (objects).<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand the difference between call by value and call by reference in Java. Learn how data is passed to methods and how changes affect original variables<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":454,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions\/454"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}