When an object is passed as an argument to a method what is actually passed to the method?

From the course: Java Memory Management

How objects are passed

- [Instructor] Passing variables into methods by value can sometimes however be confusing. And that's because when you call a method and you pass in an object as the parameter rather than a primitive data type like we see here then the variable that contains the reference to the object is what is passed. To state this more fully, when an object is passed into a method as a parameter, a copy of the variable on the stack containing the reference to the object is passed. And it's this variable on the stack that is passed by value. It means that a copy of the pointer to the object is created, the object itself is not copied and in fact it's not the object that's passed into a method but rather a pointer to the object. For example, if we pass a copy of a list into a method, then the parameter value will be a copy of the myList variable which is a pointer to the object on the heap. So I hope that's clear, many programmers misinterpret this as objects are passed by reference. But they're not, and let's look at an example of why this is significant. Here's some code which creates a customer and then calls a method which changes the customer's name and prints out the customer's name at the end. Assume that the customer class has been well written and it has get and set methods as you'd expect. What would the outputs of this be? I suggest you pause the video and work through this example using the stack and heap pictures that I've been showing you to work out what would happen. When you've had a think about it press resume on the video and I'll talk it through. So let's talk through this example. First of all, we have a line that creates the new customer object. The constructor of the customer takes a string so the first thing that will happen is a string will be created on the heap, then the customer will be created with a property that references the string object and a variable will be created on the stack pointing to this customer object. When we call rename customer because we passed the value of the customer by value, a new variable will be created on the stack called cust which will be given a copy of the pointer to the customer on the heap. Now we change the name of the customer. I hope you remember that strings are immutable in Java, that is you can't change the value of a string if you do this you actually create a new string object. So what happens when this line runs is that a new string object is created on the heap and Java changes the pointer from the name in the customer object to this new string. The original string object is no longer referenced from anywhere and so it can be garbage collected at some point in the future. Finally, we exit the rename customer method and go to the print line. And now our C variable references the amended object customer which has a name pointing to the new string Diane so the output should be the name Diane. I hope you were able to predict this outcome, if you were then well done as this means that you now do understand the mechanism of how variables are passed between methods.

Contents

I will call what you are passing in a to a function the actual parameters, and where you receive them, the parameters in the function, the formal parameters. They are also called actual and formal arguments.

When passing parameters, what it is called and what happens can be confusing. It is less essential that you call it the "correct" thing than you know exactly what is happening. It is critical to have a good mental model, a valid memory picture of the process.

Recall that when you call a function, a chunk of memory called an activation record is allocated. Critical to the discussion here is that this memory holds the formal parameter values and function local variables.

By definition, pass by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. Use pass by value when when you are only "using" the parameter for some computation, not changing it for the client program.

In pass by reference [also called pass by address], a copy of the address of the actual parameter is stored. Use pass by reference when you are changing the parameter passed in by the client program.

Consider a swapping function to demonstrate pass by value vs. pass by reference. This function, which swaps ints, cannot be done in Java.

   main[] {
      int i = 10, j = 20;
      swapThemByVal[i, j];
      cout 

Chủ Đề