{"id":88833,"date":"2024-01-25T18:00:11","date_gmt":"2024-01-25T12:30:11","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=88833"},"modified":"2024-01-25T18:00:11","modified_gmt":"2024-01-25T12:30:11","slug":"call-by-value-and-call-by-reference-in-java","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/","title":{"rendered":"Call by Value and Call by Reference in Java"},"content":{"rendered":"<p>In Java, you can make calls that are based on value rather than by reference. This is called by a value when we call methods for passing values. No calls will be affected by the modifications to call methods. When a method is called by reference, the parameter is used as the method&#8217;s reference. This will send a reference to an argument to the parameter.<\/p>\n<h2>Call by value and Call by reference in Java<\/h2>\n<p>Call by value means to call a method with a parameter as the value. This allows the value of an argument to be converted into a parameter. Call by reference implies calling method with parameter as a reference. This results in the parameter being passed as a reference to the argument.<\/p>\n<p>When called by value, the modification of the passed parameter is not reflected in the scope of the caller, while when called by reference, modifications of the passed parameter are persistent and the changes are reflected in the scope of the caller.<\/p>\n<h3>Call by value in Java:<\/h3>\n<p>The original value shall not change if calls are made by value. We&#8217;ll take a simple one,<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class TechVidvan{\n    public static void increment(int number){\n        number = number+1;  \n        System.out.println(\"value in method: \"+number);\n    }\n    public static void main(String[] args){\n        int number=10;\n        System.out.println(\"value before method call : \"+number);\n        increment(number);\n        System.out.println(\"value after method call: \"+number);\n    }\n}<\/pre>\n<h3>Call By Reference in Java:<\/h3>\n<p>Java only uses calls by value when passing reference variables. It provides a copy of that reference, which is an asset for the methods. As this reference references the same object address, it is not harmful to create a duplicate of that reference. If the new object has been placed in a reference, it will not be reflected.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Vidvan{\n   public static void main(String[] args) {\n       TechVidvan a = new TechVidvan(30);\n        TechVidvan b = new TechVidvan(45);\n      System.out.println(\"Before swapping, a = \" + a.a + \" and b = \" + b.a);\n      swapFunction(a, b);\n      System.out.println(\"**Now, Before and After swapping values will be different here**:\");\n      System.out.println(\"After swapping, a = \" + a.a + \" and b is \" + b.a);\n   }\n   public static void swapFunction(TechVidvan a, TechVidvan b) {\n      System.out.println(\"Before swapping(Inside), a = \" + a.a + \" b = \" + b.a);\n      \/\/ Swap n1 with n2\n      TechVidvan c = new TechVidvan(a.a);\n      a.a = b.a;\n      b.a = c.a;\n      System.out.println(\"After swapping(Inside), a = \" + a.a + \" b = \" + b.a);\n   }\n}\nclass TechVidvan {\n   public int a;\n   public TechVidvan(int a){ this.a = a;}\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nBefore swapping, a = 30 and b = 45<br \/>\nBefore swapping(Inside), a = 30 b = 45<br \/>\nAfter swapping(Inside), a = 45 b = 30<br \/>\n**Now, Before and After swapping values will be different here**:<br \/>\nAfter swapping, a = 45 and b is 30<\/p>\n<h3>What does the Call by Value mean?<\/h3>\n<p>The value of an argument is transferred into a function formally parameter using the call by value method. Therefore, the argument does not change as a result of changing the primary function parameter.<\/p>\n<p>In this parameter passing mechanism, values of real parameters are passed to the function&#8217;s formal parameters and they are kept in a variety of memory locations. As a result, the caller&#8217;s actual argument does not reflect any changes made in the inside functions.<\/p>\n<h3>What does the Call by Reference mean?<\/h3>\n<p>The call of the reference technique copies an argument address to a formal parameter. To obtain the true argument of a function call, which is used in this method, use an address. It indicates that any changes made to the parameter will affect a passing argument.<\/p>\n<p>The allocation of memory is the same as the arguments in the present case. The function will run all its operations on a value stored in the real parameter&#8217;s location, and an updated value is saved to that address.<\/p>\n<h3>The difference between a call of value and a call of reference in Java:<\/h3>\n<p>The differences in call by value and call by reference methods of parameter passing are set out in the table below.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Call By Value<\/b><\/td>\n<td><span style=\"font-weight: 400\"> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><b>Call By Reference<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">While calling a function, we pass the values of variables to it. Such functions are known as \u201cCall By Values\u201d.<\/span><\/td>\n<td><span style=\"font-weight: 400\">When you call a function, instead of passing the values of variables, you pass the address of the variable&#8217;s location to the function known as the Calling By References function.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> The values of every variable in the call function are copied to corresponding dummy variables on the called function using this method.\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method copies to the dummy variables of the called function the address of the actual variables in the calling function.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">This method does not affect the values of real variables in calling function&#8217;s calls when you modify a dummy variable within called function.\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">This method allows us to access the real variables using addresses, which means we can manipulate them.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Using function calls in callbyvalues is not possible to change the values of real variables.<\/span><\/td>\n<td><span style=\"font-weight: 400\"> If we call by reference using function calls, the variables&#8217; values can be changed.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">The variable values are received using an easy method.<\/span><\/td>\n<td><span style=\"font-weight: 400\"> A pointer variable must be defined to store fixed address values.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> It is preferable to use this method when we are required to pass some minor values that can&#8217;t be changed.<\/span><\/td>\n<td><span style=\"font-weight: 400\"> It is preferable to use this technique if we wish to transfer a large number of data into the function.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Advantages of Using the Call-by-Value Method:<\/h3>\n<p><strong>This approach ensures data is preserved by not altering the initial variable.<\/strong><\/p>\n<ul>\n<li>The contents of parameters should always be unchanged when a function is called.<\/li>\n<li>Consequently, any changes in formal argument will have no impact on real-world situations because the value of actual arguments is passed down to official arguments.<\/li>\n<\/ul>\n<h3>Advantages of Using the Call-by-Reference Method:<\/h3>\n<ul>\n<li>The function is able to modify the value of an argument, which is quite useful.<\/li>\n<li>When only one value is stored, it does not generate duplicate data and allows you to save memory.<\/li>\n<li>In this case, there is no duplication of arguments.<\/li>\n<li>It thus processes in a relatively quick manner, helping you to avoid any changes due to an accident.<\/li>\n<li>The person who reads the code does not know that it is possible to change the value of a function.<\/li>\n<\/ul>\n<h3>Disadvantages of Adopting the Call by Value Approach:<\/h3>\n<ul>\n<li>Argument variables can be affected as well by changes to the actual parameters.<\/li>\n<li>In this procedure, the arguments have to be variables. You can&#8217;t simply change a variable in the function body.<\/li>\n<li>Arguments can be hard to understand sometimes. Memory is wasted when two copies of the same variable are created.<\/li>\n<\/ul>\n<h3>Disadvantages of Using the Call by Reference Method:<\/h3>\n<ul>\n<li>It&#8217;s a strong reassurance of nonnullity. The function must be able to ensure that the input is not invalid when accepting a reference.<\/li>\n<li>Therefore, it is not necessary to perform a null check. This function is no longer pure semantically because it&#8217;s being transmitted by reference.<\/li>\n<li>The major issue is the lifetime guarantee in relation to references. This is very dangerous when you use lambdas and multithreaded applications.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>Both C++ and Java are used in this way according to what&#8217;s provided. If you want to see how the original value of a variable changes, use the &#8216;call by value&#8217; technique or &#8216;call by reference&#8217; method if you only want to send an initial value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Java, you can make calls that are based on value rather than by reference. This is called by a value when we call methods for passing values. No calls will be affected by&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":89054,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5347,5348,5349,5350,5351,296,5352,299],"class_list":["post-88833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-call-by-reference-in-java","tag-call-by-value-and-call-by-reference-in-java","tag-call-by-value-and-reference-in-java","tag-call-by-value-in-java","tag-difference-between-call-by-value-and-call-by-reference","tag-java","tag-java-call-by-value-and-call-by-reference","tag-java-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Call by Value and Call by Reference in Java - TechVidvan<\/title>\n<meta name=\"description\" content=\"Call by value means to call a method with a parameter as the value. Call by reference implies calling method with parameter as a reference.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Call by Value and Call by Reference in Java - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Call by value means to call a method with a parameter as the value. Call by reference implies calling method with parameter as a reference.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-25T12:30:11+00:00\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Call by Value and Call by Reference in Java - TechVidvan","description":"Call by value means to call a method with a parameter as the value. Call by reference implies calling method with parameter as a reference.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Call by Value and Call by Reference in Java - TechVidvan","og_description":"Call by value means to call a method with a parameter as the value. Call by reference implies calling method with parameter as a reference.","og_url":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-01-25T12:30:11+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Call by Value and Call by Reference in Java","datePublished":"2024-01-25T12:30:11+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/"},"wordCount":1069,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/#primaryimage"},"thumbnailUrl":"","keywords":["call by reference in java","call by value and call by reference in java","call by value and reference in java","call by value in java","difference between call by value and call by reference","java","java call by value and call by reference","Java Tutorial"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/","url":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/","name":"Call by Value and Call by Reference in Java - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/#primaryimage"},"thumbnailUrl":"","datePublished":"2024-01-25T12:30:11+00:00","description":"Call by value means to call a method with a parameter as the value. Call by reference implies calling method with parameter as a reference.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/call-by-value-and-call-by-reference-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Call by Value and Call by Reference in Java"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=88833"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88833\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=88833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=88833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=88833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}