{"id":3197,"date":"2017-03-31T11:49:29","date_gmt":"2017-03-31T06:19:29","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3197"},"modified":"2017-03-31T11:49:29","modified_gmt":"2017-03-31T06:19:29","slug":"java-pass-reference-pass-value","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-pass-reference-pass-value\/","title":{"rendered":"JAVA &#8211; Is Java pass-by-reference or  pass-by-value ?"},"content":{"rendered":"<h4 id=\"pass-by-value\"><span style=\"color: #800000;\">Pass-by-value<\/span><\/h4>\n<ul>\n<li>The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter\u2019s value during method\/function execution.<\/li>\n<li>That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it), but other languages could choose parameter storage differently.<\/li>\n<\/ul>\n<h4 id=\"pass-by-reference\"><span style=\"color: #800080;\">Pass-by-reference<\/span><\/h4>\n<ul>\n<li>The formal parameter merely acts as an alias for the actual parameter. Anytime the method\/function uses the formal parameter (for reading or writing), it is actually using the actual parameter.<\/li>\n<li>Java passes\u00a0everything\u00a0by value, and not by reference \u2013 make sure you remember that. And when we say everything, we mean everything \u2013 objects, arrays (which are objects in Java), primitive types (like ints and floats), etc. \u2013 these are\u00a0all passed by value\u00a0in Java.<\/li>\n<\/ul>\n<p>Java is always\u00a0pass-by-value. Unfortunately, they decided to call pointers references, thus confusing beginners. Because those\u00a0references\u00a0are passed by value. For Example,<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dpublic%20static%20void%20main(%20String%5B%5D%20args%20)%7B%0A%20%20%20%20Dog%20aDog%20%3D%20new%20Dog(%22Max%22)%3B%0A%20%20%20%20foo(aDog)%3B%0A%0A%20%20%20%20if%20(aDog.getName().equals(%22Max%22))%20%7B%20%2F%2Ftrue%0A%20%20%20%20%20%20%20%20System.out.println(%20%22Java%20passes%20by%20value.%22%20)%3B%0A%0A%20%20%20%20%7D%20else%20if%20(aDog.getName().equals(%22Fifi%22))%20%7B%0A%20%20%20%20%20%20%20%20System.out.println(%20%22Java%20passes%20by%20reference.%22%20)%3B%0A%20%20%20%20%7D%0A%7D%0Apublic%20static%20void%20foo(Dog%20d)%20%7B%0A%20%20%20%20d.getName().equals(%22Max%22)%3B%20%2F%2F%20true%0A%0A%20%20%20%20d%20%3D%20new%20Dog(%22Fifi%22)%3B%0A%20%20%20%20d.getName().equals(%22Fifi%22)%3B%20%2F%2F%20true%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>In this example aDog.getName() will still return \u201cMax\u201d. The value aDog within main is not overwritten in the function foo with the Dog \u201cFifi\u201d as the object reference is passed by value. If it were passed by reference, then the aDog.getName() in main would return \u201cFifi\u201d after the call to foo.<\/p>\n<p><span style=\"color: #000000;\"><strong>Likewise:<\/strong><\/span><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dDog%20aDog%20%3D%20new%20Dog(%22Max%22)%3B%20%0Afoo(aDog)%3B%20%0AaDog.getName().equals(%22Fifi%22)%3B%20%2F%2F%20true%20%0Apublic%20void%20foo(Dog%20d)%20%0A%7B%20%0Ad.getName().equals(%22Max%22)%3B%20%2F%2F%20true%0A%20d.setName(%22Fifi%22)%3B%20%0A%7D%20\u2033 message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>In this example, FiFi is the dog\u2019s name after call to foo(aDog). Any operations that foo performs on d are such that, for all practical purposes, they are performed on aDog itself (except d=new Dog(\u201cBoxer\u201d)).<\/p>\n<p><strong>Another Example that clearly defines java always pass-by-value and not by reference<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dpublic%20class%20Main%7B%0A%20%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%7B%0A%20%20%20%20%20%20%20%20%20%20Foo%20f%20%3D%20new%20Foo(%22f%22)%3B%0A%20%20%20%20%20%20%20%20%20%20changeReference(f)%3B%20%2F%2F%20It%20won\u2019t%20change%20the%20reference!%0A%20%20%20%20%20%20%20%20%20%20modifyReference(f)%3B%20%2F%2F%20It%20will%20modify%20the%20object%20that%20the%20reference%20variable%20%22f%22%20refers%20to!%0A%20%20%20%20%20%7D%0A%20%20%20%20%20public%20static%20void%20changeReference(Foo%20a)%7B%0A%20%20%20%20%20%20%20%20%20%20Foo%20b%20%3D%20new%20Foo(%22b%22)%3B%0A%20%20%20%20%20%20%20%20%20%20a%20%3D%20b%3B%0A%20%20%20%20%20%7D%0A%20%20%20%20%20public%20static%20void%20modifyReference(Foo%20c)%7B%0A%20%20%20%20%20%20%20%20%20%20c.setAttribute(%22c%22)%3B%0A%20%20%20%20%20%7D%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>Declaring a reference named f of type Foo and assign it to a new object of type Foo with an attribute \u201cf\u201d.<br \/>\nFoo f = new Foo(\u201cf\u201d);<\/li>\n<li>From the method side, a reference of type Foo with a name a is declared and it\u2019s initially assigned to null.<br \/>\npublic static void changeReference(Foo a)<\/li>\n<li>As you call the method changeReference, the reference a will be assigned to the object which is passed as an argument.<br \/>\nchangeReference(f);<\/li>\n<li>Declaring a reference named b of type Foo and assign it to a new object of type Foo with an attribute \u201cb\u201d.<br \/>\nFoo b = new Foo(\u201cb\u201d);<\/li>\n<li>a = b is re-assigning the reference a NOT f to the object whose its attribute is \u201cb\u201c<\/li>\n<li>As you call modifyReference(Foo c) method, a reference c is created and assigned to the object with attribute \u201cf\u201d.<\/li>\n<li>c.setAttribute(\u201cc\u201d); will change the attribute of the object that reference c points to it, and it\u2019s same object that reference f points to it.<\/li>\n<li>Thus this is represented diagrammatically,<\/li>\n<\/ul>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-3223 size-full aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/2017-03-30_132134.png\" alt=\"\" width=\"883\" height=\"398\" \/><\/p>\n<p>\u00a0<\/p>\n<p>Java does manipulate objects by reference, and all object variables are references. However, Java doesn\u2019t pass method arguments by reference; it passes them by value.<\/p>\n<p>Take the badSwap() method for example:<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dpublic%20void%20badSwap(int%20var1%2C%20int%20var2)%0A%7B%0A%20%20int%20temp%20%3D%20var1%3B%0A%20%20var1%20%3D%20var2%3B%0A%20%20var2%20%3D%20temp%3B%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>When badSwap() returns, the variables passed as arguments will still hold their original values. The method will also fail if we change the arguments type from int to Object, since Java passes object references by value as well. Now, here is where it gets tricky:<\/li>\n<\/ul>\n[pastacode lang=\u201djava\u201d manual=\u201dpublic%20void%20tricky(Point%20arg1%2C%20Point%20arg2)%0A%7B%0A%20%20arg1.x%20%3D%20100%3B%0A%20%20arg1.y%20%3D%20100%3B%0A%20%20Point%20temp%20%3D%20arg1%3B%0A%20%20arg1%20%3D%20arg2%3B%0A%20%20arg2%20%3D%20temp%3B%0A%7D%0Apublic%20static%20void%20main(String%20%5B%5D%20args)%0A%7B%0A%20%20Point%20pnt1%20%3D%20new%20Point(0%2C0)%3B%0A%20%20Point%20pnt2%20%3D%20new%20Point(0%2C0)%3B%0A%20%20System.out.println(%22X%3A%20%22%20%2B%20pnt1.x%20%2B%20%22%20Y%3A%20%22%20%2Bpnt1.y)%3B%20%0A%20%20System.out.println(%22X%3A%20%22%20%2B%20pnt2.x%20%2B%20%22%20Y%3A%20%22%20%2Bpnt2.y)%3B%0A%20%20System.out.println(%22%20%22)%3B%0A%20%20tricky(pnt1%2Cpnt2)%3B%0A%20%20System.out.println(%22X%3A%20%22%20%2B%20pnt1.x%20%2B%20%22%20Y%3A%22%20%2B%20pnt1.y)%3B%20%0A%20%20System.out.println(%22X%3A%20%22%20%2B%20pnt2.x%20%2B%20%22%20Y%3A%20%22%20%2Bpnt2.y)%3B%20%20%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><strong>output:<\/strong><\/p>\n<blockquote><p>X: 0 Y: 0<br \/>\nX: 0 Y: 0<br \/>\nX: 100 Y: 100<br \/>\nX: 0 Y: 0<\/p><\/blockquote>\n<ul>\n<li>The method successfully alters the value of pnt1, even though it is passed by value; however, a swap of pnt1 and pnt2 fails! This is the major source of confusion. In the main() method, pnt1 and pnt2 are nothing more than object references.<\/li>\n<li>When you pass pnt1 and pnt2 to the tricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies of the original references.<\/li>\n<li>Figure 1 below shows two references pointing to the same object after Java passes an object to a method. This diagram shows After being passed to a method, an object will have at least two references<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"wp-image-3215 size-full aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture8.png\" alt=\"\" width=\"200\" height=\"120\" \/><\/p>\n<ul>\n<li>Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects.<\/li>\n<li>But since the references are copies, swaps will fail. As Figure 2 illustrates, the method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references.<\/li>\n<li>For a swap to succeed outside of the method call, we need to swap the original references, not the copies.<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"size-medium wp-image-3216 aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture9-236x300.png\" alt=\"\" width=\"236\" height=\"300\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture9-236x300.png 236w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture9.png 322w\" sizes=\"(max-width: 236px) 100vw, 236px\" \/><\/p>\n<ul>\n<li>\u201cJava manipulates objects \u2018by reference,\u2019 but it passes object references to methods \u2018by value.'\u201d As a result, you cannot write a standard swap method to swap objects.<\/li>\n<li>Example of pass by value in Java<\/li>\n<\/ul>\n<p>Suppose we have a method that is named \u201cReceiving\u201d and it expects an integer to be passed to it:<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dpublic%20void%20Receiving%20(int%20var)%0A%7B%0A%20%20var%20%3D%20var%20%2B%202%3B%0A%7D%0ANote%20that%20the%20%E2%80%9Cvar%E2%80%9D%20variable%20has%202%20added%20to%20it.%20Now%2C%20suppose%20that%20we%20have%20some%20code%20which%20calls%20the%20method%20Receiving%3A%0A%0Apublic%20static%20void%20main(String%20%5B%5D%20args)%0A%7B%0A%20%20int%20passing%20%3D%203%3B%0A%0A%20%20Receiving%20(passing)%3B%0A%20%0A%20%20System.out.println(%22The%20value%20of%20passing%20is%3A%20%22%20%2B%20passing)%3B%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>In the main method, we set the variable \u201cpassing\u201d to 3, and then pass that variable to the Receiving method, which then adds 2 to the variable passed in.<\/li>\n<li>What do you think the \u201cSystem.out.println\u201d call will print out? Well, if you thought it would print out a \u201c5\u201d you are wrong. It will actually print out a \u201c3\u201d. Why does it print out a 3 when we clearly added a 2 to the value in the Receiving method?<\/li>\n<li>The reason it prints out a \u201c3\u201d is because Java passes arguments by value \u2013 which means that when the call to \u201cReceiving\u201d is made, Java will just create a copy of the \u201cpassing\u201d variable and that copy is what gets passed to the Receiving method \u2013 and not the original variable stored in the \u201cmain\u201d method.<\/li>\n<li>This is why whatever happens to \u201cvar\u201d inside the Receiving method does not affect the \u201cpassing\u201d variable inside the main method.<\/li>\n<\/ul>\n<h4 id=\"how-does-pass-by-value-work\"><span style=\"color: #800080;\">How does pass by value work?<\/span><\/h4>\n<ul>\n<li>Java basically creates another integer variable with the value of 3, and passes that to the \u201cReceiving\u201d method. That is why it is called \u201cpass by value\u201d \u2013 because the value of 3 is what is being passed to the \u201cReceiving\u201d method, not a reference to the \u201cpassing\u201d variable.<\/li>\n<\/ul>\n<h4 id=\"are-objects-passed-by-reference-in-java\"><span style=\"color: #0000ff;\">Are objects passed by reference in Java?<\/span><\/h4>\n<ul>\n<li>Objects are not passed by reference in Java. Let\u2019s be a little bit more specific by what we mean here: objects are passed by reference \u2013 meaning that a reference\/memory address is passed when an object is assigned to another \u2013 BUT (and this is what\u2019s important) that reference is actually passed by value.<\/li>\n<li>The reference is passed by value because a copy of the reference value is created and passed into the other object \u2013 read this entire article and this will make a lot more sense. So objects are still passed by value in Java, just like everything else.<\/li>\n<li>One other very important thing to know is that a variable of a class type stores an address of the object, and not the object itself. The object itself is stored at the address which the variable points to. Let\u2019s suppose we have a class called SomeClass and that we instantiate it with a variable of the SomeClass type.<\/li>\n<\/ul>\n<p><strong>So, suppose we have the following very simple code:<\/strong><\/p>\n<p>SomeClass someVar;<\/p>\n<ul>\n<li>The someVar variable above is a variable of a class type \u2013 which is commonly referred to as an object. But, what actually happens behind the scenes is that someVar will just contain a reference \u2013 which is basically a memory address that points to the real object and all the class variables for that object actually lives. So, remember that a variable of a class type \u2013 or what is commonly referred to as an object of a class \u2013 really just stores a memory address that points to the real object. Bottom line: anytime you see a variable of a class type like the someVar variable above, just remember that it is basically a reference that stores an address in memory.<\/li>\n<li>An example and some diagrams will help make this very clear. Suppose we have a class called PersonClass, which has a method called set that just sets the name and age for a given Person object. The details of the class itself don\u2019t matter for the purpose of this example so we won\u2019t bother to show the implementation of PersonClass.<\/li>\n<\/ul>\n<p>Now, let\u2019s say we have the following code:<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2Fcreate%20an%20object%20by%20passing%20in%20a%20name%20and%20age%3A%0APersonClass%20variable1%20%3D%20new%20PersonClass(%22Mary%22%2C%2032)%3B%0A%0APersonClass%20variable2%3B%0A%0A%2F%2F%20Both%20variable2%20and%20variable1%20now%20both%20name%20the%20same%20object%0Avariable2%20%3D%20variable1%3B%20%0A%0A%2F*this%20also%20changes%20variable1%2C%20since%20variable%202%20and%20variable1%0A%20%20%20name%20the%20same%20exact%20object%3A%20*%2F%0A%0Avariable2.set(%22Jack%22%2C%2022)%3B%0A%0ASystem.out.println(variable1)%3B\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>Let\u2019s just assume that System.out.println method above will print the name and age of a PersonClass object \u2013 even though this is not correct, it does keep the example simple and easy to understand. So, if we run the code above, the output will be:<\/li>\n<\/ul>\n<p>Jack 22<\/p>\n<ul>\n<li>In the code above, you can see that when we made a change to variable2, that it also changed variable1. This might confuse you into thinking that because of that fact Java is pass by reference \u2013 and you would be very WRONG.<\/li>\n<li>Both variable1 and variable2 hold a reference to the same object in memory \u2013 and this happened when we ran this line of code \u201cvariable2 = variable1\u2033. When we use the assignment operator (the \u201c=\u201d) with variables of a class type, then we are copying that reference value \u2013 in other words variable2 is given the memory address (or reference) that is being stored in variable1.<\/li>\n<li>Remember that the word reference means the same thing as memory address.<\/li>\n<\/ul>\n<p><strong>This assignment of references is best displayed by actual drawings, which we show below:<\/strong><\/p>\n<p>An illustration of pass by value in Java<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-3217 aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture10-300x267.png\" alt=\"\" width=\"300\" height=\"267\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture10-300x267.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture10.png 540w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<ul>\n<li>So, looking at the images above it should be clear that variable1 and variable2 are just two different references to the same exact spot in memory. Because of the fact that the statement \u201cvariable2 = variable1\u2033 essentially just copies the reference from variable1 into variable2, this is pass by value.<\/li>\n<li>You should think of the reference (which is a memory address) as the value, and in this case that reference is what is being copied.<\/li>\n<li>And, that is exactly what pass by value means \u2013 a copy of a value is passed to another variable.<\/li>\n<\/ul>\n<p>You should also read our article on the differences between primitive types and reference types here:<\/p>\n<ul>\n<li>Primitive type vs Reference type.(http:\/\/www.programmerinterview.com\/index.php\/java-questions\/difference-between-a-primitive-type-and-a-class-type\/)<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-3218 aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture11-300x267.png\" alt=\"\" width=\"300\" height=\"267\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture11-300x267.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture11.png 540w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2Fcreate%20an%20object%20by%20passing%20in%20a%20name%20and%20age%3A%0APersonClass%20variable1%20%3D%20new%20PersonClass(%22Mary%22%2C%2032)%3B%0A%0APersonClass%20variable2%3B%0A%0A%2F%2FBoth%20variable2%20and%20variable1%20now%20reference%20the%20same%20object%0Avariable2%20%3D%20variable1%3B%20%0A%0APersonClass%20variable3%20%3D%20new%20PersonClass(%22Andre%22%2C%2045)%3B%0A%0A%2F%2F%20variable1%20now%20points%20to%20variable3%0Avariable1%20%3D%20variable3%3B%0A%0A%2F%2FWHAT%20IS%20OUTPUT%20BY%20THIS%3F%0ASystem.out.println(variable2)%3B%0ASystem.out.println(variable1)%3B\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><strong>If we run the code above it will output the code below:<\/strong><\/p>\n<p>Mary 32<br \/>\nAndre 45<\/p>\n<ul>\n<li>The key to understanding the code above is the fact that changing the object that variable1 points to does not change variable2.<\/li>\n<li>So, even though variable1 is changed to point to a different object, that has no effect whatsoever on variable2. Hopefully that is clear to you \u2013 that variable1 and variable2 are not interchangeable \u2013 they are different variables that just store the same value \u2013 at least until the \u201cvariable1 = variable3\u201d statement.<\/li>\n<li>And that should prove to you that Java passes objects by value, and everything else for that matter.<\/li>\n<\/ul>\n<p><strong>Is it possible to pass an object by reference in Java?<\/strong><\/p>\n<ul>\n<li>No, there are no \u201cextra\u201d language features in Java that would allow for you to pass an object by reference in Java. Java is strictly pass by value, and does not give the programmer the option of passing anything by reference.<\/li>\n<\/ul>\n<p><strong>Are arrays passed by reference in Java?<\/strong><\/p>\n<ul>\n<li>Arrays in Java are also objects. And what did you learn about objects in Java? Well, that they are passed by value and not passed by reference.<\/li>\n<li>And the same is true for arrays in Java \u2013 they are passed by value and not by reference. Of course, like any other class object, when an array is passed to another method that method can still change the contents of the array itself.<\/li>\n<li>But, what is being passed to the method is a copy of the reference address that points to the array object. Just take a look at our example above for the PersonClass \u2013 the same exact principles apply to arrays since they are objects in Java, and are passed by value.<\/li>\n<\/ul>\n<p><strong>A Note on Remote Method Invocation (RMI)<\/strong><\/p>\n<ul>\n<li>When passing parameters to remote methods, things get a bit more complex. First, we\u2019re (usually) dealing with passing data between two independent virtual machines, which might be on separate physical machines as well.<\/li>\n<li>Passing the value of a pointer wouldn\u2019t do any good, as the target virtual machine doesn\u2019t have access to the caller\u2019s heap.<\/li>\n<li>You\u2019ll often hear \u201cpass by value\u201d and \u201cpass by reference\u201d used with respect to RMI. These terms have more of a \u201clogical\u201d meaning, and really aren\u2019t correct for the intended use.<\/li>\n<li>Here\u2019s what is usually meant by these phrases with regard to RMI.<\/li>\n<\/ul>\n<p>Note that this is not proper usage of \u201cpass by value\u201d and \u201cpass by reference\u201d semantics:<\/p>\n<h4 id=\"rmi-pass-by-value\"><span style=\"color: #ff6600;\"><strong>RMI Pass-by-value<\/strong><\/span><\/h4>\n<ul>\n<li>The actual parameter is serialized and passed using a network protocol to the target remote object. Serialization essentially \u201csqueezes\u201d the data out of an object\/primitive. On the receiving end, that data is used to build a \u201cclone\u201d of the original object or primitive.<\/li>\n<li>Note that this process can be rather expensive if the actual parameters point to large objects (or large graphs of objects).<\/li>\n<li>This isn\u2019t quite the right use of \u201cpass-by-value\u201d; I think it should really be called something like \u201cpass-by-memento\u201d. (See \u201cDesign Patterns\u201d by Gamma et al for a description of the Memento pattern).<\/li>\n<\/ul>\n<h4 id=\"rmi-pass-by-reference\"><span style=\"color: #993300;\"><strong>RMI Pass-by-reference<\/strong><\/span><\/h4>\n<ul>\n<li>The actual parameter, which is itself a remote object, is represented by a proxy. The proxy keeps track of where the actual parameter lives, and anytime the target method uses the formal parameter, another remote method invocation occurs to \u201ccall back\u201d to the actual parameter.<\/li>\n<li>This can be useful if the actual parameter points to a large object (or graph of objects) and there are few call backs.<br \/>\nThis isn\u2019t quite the right use of \u201cpass-by-reference\u201d (again, you cannot change the actual parameter itself).<\/li>\n<li>I think it should be called something like \u201cpass-by-proxy\u201d. (Again, see \u201cDesign Patterns\u201d for descriptions of the Proxy pattern).<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Pass-by-value The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter\u2019s value during method\/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it), but other languages could [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2139],"tags":[6024,6026,6017,6016,6028,6027,6025,6023,6022,6019,6018,6020,6021],"class_list":["post-3197","post","type-post","status-publish","format-standard","hentry","category-java","tag-clarification-on-the-statement-object-references-are-passed-by-values-in-java","tag-does-java-pass-by-reference-or-by-value","tag-how-do-i-pass-a-variable-by-reference","tag-is-javascript-a-pass-by-reference-or-pass-by-value-language","tag-java-is-pass-by-value-and-not-pass-by-reference","tag-java-pass-by-value-and-pass-by-reference","tag-java-pass-by-value-and-reference","tag-java-pass-by-value-references-in-methods","tag-javaandroid-passing-objects-to-methods-with-resources-open","tag-javascript-by-reference-vs-by-value","tag-passing-a-string-by-reference-in-java","tag-passing-objects-by-reference-or-value-in-c","tag-passing-objects-by-reference-vs-value"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3197"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3197\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}